diff --git "a/problems/0039.347円273円204円345円220円210円346円200円273円345円222円214円.md" "b/problems/0039.347円273円204円345円220円210円346円200円273円345円222円214円.md" index 564d13ea3e..54e9f2e5c1 100644 --- "a/problems/0039.347円273円204円345円220円210円346円200円273円345円222円214円.md" +++ "b/problems/0039.347円273円204円345円220円210円346円200円273円345円222円214円.md" @@ -291,7 +291,7 @@ class Solution: for i in range(start_index, len(candidates)): sum_ += candidates[i] self.path.append(candidates[i]) - self.backtracking(candidates, target, sum_, i) # 因为无限制重复选取,所以不是i-1 + self.backtracking(candidates, target, sum_, i) # 因为无限制重复选取,所以不是i+1 sum_ -= candidates[i] # 回溯 self.path.pop() # 回溯 ``` diff --git "a/problems/1382.345円260円206円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円345円217円230円345円271円263円350円241円241円.md" "b/problems/1382.345円260円206円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円345円217円230円345円271円263円350円241円241円.md" index 631ddbef8a..7d983fbb1f 100644 --- "a/problems/1382.345円260円206円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円345円217円230円345円271円263円350円241円241円.md" +++ "b/problems/1382.345円260円206円344円272円214円345円217円211円346円220円234円347円264円242円346円240円221円345円217円230円345円271円263円350円241円241円.md" @@ -188,6 +188,30 @@ var balanceBST = function(root) { }; ``` +TypeScript: + +```typescript +function balanceBST(root: TreeNode | null): TreeNode | null { + const inorderArr: number[] = []; + inorderTraverse(root, inorderArr); + return buildTree(inorderArr, 0, inorderArr.length - 1); +}; +function inorderTraverse(node: TreeNode | null, arr: number[]): void { + if (node === null) return; + inorderTraverse(node.left, arr); + arr.push(node.val); + inorderTraverse(node.right, arr); +} +function buildTree(arr: number[], left: number, right: number): TreeNode | null { + if (left> right) return null; + const mid = (left + right)>> 1; + const resNode: TreeNode = new TreeNode(arr[mid]); + resNode.left = buildTree(arr, left, mid - 1); + resNode.right = buildTree(arr, mid + 1, right); + return resNode; +} +``` + ----------------------- diff --git "a/problems/351円235円242円350円257円225円351円242円23002円.07.351円223円276円350円241円250円347円233円270円344円272円244円.md" "b/problems/351円235円242円350円257円225円351円242円23002円.07.351円223円276円350円241円250円347円233円270円344円272円244円.md" index 1ae01061ad..0fe37949fd 100644 --- "a/problems/351円235円242円350円257円225円351円242円23002円.07.351円223円276円350円241円250円347円233円270円344円272円244円.md" +++ "b/problems/351円235円242円350円257円225円351円242円23002円.07.351円223円276円350円241円250円347円233円270円344円272円244円.md" @@ -13,21 +13,21 @@ 图示两个链表在节点 c1 开始相交: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221657.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221657.png) 题目数据 保证 整个链式结构中不存在环。 -注意,函数返回结果后,链表必须 保持其原始结构 。 +注意,函数返回结果后,链表必须 保持其原始结构 。 -示例 1: +示例 1: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221723.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221723.png) 示例 2: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221749.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221749.png) -示例 3: +示例 3: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221812.png)![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221812.png) @@ -100,7 +100,7 @@ public: ## 其他语言版本 -### Java +### Java ```Java public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { @@ -144,11 +144,11 @@ public class Solution { } return null; } - + } ``` -### Python +### Python ```python class Solution: @@ -162,15 +162,15 @@ class Solution: """ cur_a, cur_b = headA, headB # 用两个指针代替a和b - + while cur_a != cur_b: cur_a = cur_a.next if cur_a else headB # 如果a走完了,那么就切换到b走 cur_b = cur_b.next if cur_b else headA # 同理,b走完了就切换到a - + return cur_a ``` -### Go +### Go ```go func getIntersectionNode(headA, headB *ListNode) *ListNode { @@ -208,7 +208,30 @@ func getIntersectionNode(headA, headB *ListNode) *ListNode { } ``` -### javaScript +双指针 + +```go +func getIntersectionNode(headA, headB *ListNode) *ListNode { + l1,l2 := headA, headB + for l1 != l2 { + if l1 != nil { + l1 = l1.Next + } else { + l1 = headB + } + + if l2 != nil { + l2 = l2.Next + } else { + l2 = headA + } + } + + return l1 +} +``` + +### javaScript ```js var getListLen = function(head) { @@ -218,9 +241,9 @@ var getListLen = function(head) { cur = cur.next; } return len; -} +} var getIntersectionNode = function(headA, headB) { - let curA = headA,curB = headB, + let curA = headA,curB = headB, lenA = getListLen(headA), lenB = getListLen(headB); if(lenA < lenB) {

AltStyle によって変換されたページ (->オリジナル) /