From 552388e8fdb3439fc3222cb0dafa02c48c0262f5 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Thu, 6 Jan 2022 13:58:34 +0800 Subject: [PATCH 01/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880059.=E8=9E=BA?= =?UTF-8?q?=E6=97=8B=E7=9F=A9=E9=98=B5II.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0059.螺旋矩阵II.md | 45 +++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/problems/0059.螺旋矩阵II.md b/problems/0059.螺旋矩阵II.md index c7e91b4d..3f7a59ca 100644 --- a/problems/0059.螺旋矩阵II.md +++ b/problems/0059.螺旋矩阵II.md @@ -287,6 +287,51 @@ var generateMatrix = function(n) { ``` +TypeScript: + +```typescript +function generateMatrix(n: number): number[][] { + let loopNum: number = Math.floor(n / 2); + const resArr: number[][] = new Array(n).fill(1).map(i => new Array(n)); + let chunkNum: number = n - 1; + let startX: number = 0; + let startY: number = 0; + let value: number = 1; + let x: number, y: number; + while (loopNum--) { + x = startX; + y = startY; + while (x < startX + chunkNum) { + resArr[y][x] = value; + x++; + value++; + } + while (y < startY + chunkNum) { + resArr[y][x] = value; + y++; + value++; + } + while (x > startX) { + resArr[y][x] = value; + x--; + value++; + } + while (y > startY) { + resArr[y][x] = value; + y--; + value++; + } + startX++; + startY++; + chunkNum -= 2; + } + if (n % 2 === 1) { + resArr[startX][startY] = value; + } + return resArr; +}; +``` + Go: ```go From d2904c2675ecc26f0b385f368c857eeb68d526fa Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sun, 9 Jan 2022 18:19:46 +0800 Subject: [PATCH 02/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880206.=E7=BF=BB?= =?UTF-8?q?=E8=BD=AC=E9=93=BE=E8=A1=A8.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0206.翻转链表.md | 48 +++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/problems/0206.翻转链表.md b/problems/0206.翻转链表.md index 13ed753c..941928ba 100644 --- a/problems/0206.翻转链表.md +++ b/problems/0206.翻转链表.md @@ -314,6 +314,54 @@ var reverseList = function(head) { }; ``` +TypeScript: + +```typescript +// 双指针法 +function reverseList(head: ListNode | null): ListNode | null { + let preNode: ListNode | null = null, + curNode: ListNode | null = head, + tempNode: ListNode | null; + while (curNode) { + tempNode = curNode.next; + curNode.next = preNode; + preNode = curNode; + curNode = tempNode; + } + return preNode; +}; + +// 递归(从前往后翻转) +function reverseList(head: ListNode | null): ListNode | null { + function recur(preNode: ListNode | null, curNode: ListNode | null): ListNode | null { + if (curNode === null) return preNode; + let tempNode: ListNode | null = curNode.next; + curNode.next = preNode; + preNode = curNode; + curNode = tempNode; + return recur(preNode, curNode); + } + return recur(null, head); +}; + +// 递归(从后往前翻转) +function reverseList(head: ListNode | null): ListNode | null { + if (head === null) return null; + let newHead: ListNode | null; + function recur(node: ListNode | null, preNode: ListNode | null): void { + if (node.next === null) { + newHead = node; + newHead.next = preNode; + } else { + recur(node.next, node); + node.next = preNode; + } + } + recur(head, null); + return newHead; +}; +``` + Ruby: ```ruby From c5c8fe50b4f101d495e4218722335f467e7e936c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E5=AE=A2=E5=AD=A6=E4=BC=9F?= Date: Mon, 10 Jan 2022 15:02:55 +0800 Subject: [PATCH 03/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20101.=20=E5=AF=B9?= =?UTF-8?q?=E7=A7=B0=E4=BA=8C=E5=8F=89=E6=A0=91=20Swift=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0101.对称二叉树.md | 80 ++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/problems/0101.对称二叉树.md b/problems/0101.对称二叉树.md index b9dff99c..69bc41d3 100644 --- a/problems/0101.对称二叉树.md +++ b/problems/0101.对称二叉树.md @@ -574,7 +574,87 @@ var isSymmetric = function(root) { }; ``` +## Swift: +> 递归 +```swift +func isSymmetric(_ root: TreeNode?) -> Bool { + return _isSymmetric(root?.left, right: root?.right) +} +func _isSymmetric(_ left: TreeNode?, right: TreeNode?) -> Bool { + // 首先排除空节点情况 + if left == nil && right == nil { + return true + } else if left == nil && right != nil { + return false + } else if left != nil && right == nil { + return false + } else if left!.val != right!.val { + // 进而排除数值不相等的情况 + return false + } + + // left 和 right 都不为空, 且数值也相等就递归 + let inSide = _isSymmetric(left!.right, right: right!.left) + let outSide = _isSymmetric(left!.left, right: right!.right) + return inSide && outSide +} +``` + +> 迭代 - 使用队列 +```swift +func isSymmetric2(_ root: TreeNode?) -> Bool { + guard let root = root else { + return true + } + var queue = [TreeNode?]() + queue.append(root.left) + queue.append(root.right) + while !queue.isEmpty { + let left = queue.removeFirst() + let right = queue.removeFirst() + if left == nil && right == nil { + continue + } + if left == nil || right == nil || left?.val != right?.val { + return false + } + queue.append(left!.left) + queue.append(right!.right) + queue.append(left!.right) + queue.append(right!.left) + } + return true +} +``` + +> 迭代 - 使用栈 +```swift +func isSymmetric3(_ root: TreeNode?) -> Bool { + guard let root = root else { + return true + } + var stack = [TreeNode?]() + stack.append(root.left) + stack.append(root.right) + while !stack.isEmpty { + let left = stack.removeLast() + let right = stack.removeLast() + + if left == nil && right == nil { + continue + } + if left == nil || right == nil || left?.val != right?.val { + return false + } + stack.append(left!.left) + stack.append(right!.right) + stack.append(left!.right) + stack.append(right!.left) + } + return true +} +``` -----------------------
From b3711266156c2b8e25ac6e40b681842fefab9279 Mon Sep 17 00:00:00 2001 From: HanMengnan <1448189829@qq.com> Date: Tue, 11 Jan 2022 12:48:46 +0800 Subject: [PATCH 04/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=2034.=20=E5=9C=A8?= =?UTF-8?q?=E6=8E=92=E5=BA=8F=E6=95=B0=E7=BB=84=E4=B8=AD=E6=9F=A5=E6=89=BE?= =?UTF-8?q?=E5=85=83=E7=B4=A0=E7=9A=84=E7=AC=AC=E4=B8=80=E4=B8=AA=E5=92=8C?= =?UTF-8?q?=E6=9C=80=E5=90=8E=E4=B8=80=E4=B8=AA=E4=BD=8D=E7=BD=AE=20Go?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...元素的第一个和最后一个位置.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md index 2b96e41b..3f9cd747 100644 --- a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md +++ b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md @@ -389,6 +389,54 @@ class Solution: ### Go ```go +func searchRange(nums []int, target int) []int { + leftBorder := searchLeftBorder(nums, target) + rightBorder := searchRightBorder(nums, target) + + if leftBorder == -2 || rightBorder == -2 { // 情况一 + return []int{-1, -1} + } else if rightBorder-leftBorder > 1 { // 情况三 + return []int{leftBorder + 1, rightBorder - 1} + } else { // 情况二 + return []int{-1, -1} + } +} + +func searchLeftBorder(nums []int, target int) int { + left, right := 0, len(nums)-1 + leftBorder := -2 // 记录一下leftBorder没有被赋值的情况 + for left <= right { + middle := (left + right) / 2 + if target == nums[middle] { + right = middle - 1 + // 左边界leftBorder更新 + leftBorder = right + } else if target > nums[middle] { + left = middle + 1 + } else { + right = middle - 1 + } + } + return leftBorder +} + +func searchRightBorder(nums []int, target int) int { + left, right := 0, len(nums)-1 + rightBorder := -2 // 记录一下rightBorder没有被赋值的情况 + for left <= right { + middle := (left + right) / 2 + if target == nums[middle] { + left = middle + 1 + // 右边界rightBorder更新 + rightBorder = left + } else if target > nums[middle] { + left = middle + 1 + } else { + right = middle - 1 + } + } + return rightBorder +} ``` ### JavaScript From dfa64fa37b9493cfdf41a431d06da3565e86b1c5 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Tue, 11 Jan 2022 13:59:52 +0800 Subject: [PATCH 05/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880024.=E4=B8=A4?= =?UTF-8?q?=E4=B8=A4=E4=BA=A4=E6=8D=A2=E9=93=BE=E8=A1=A8=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E8=8A=82=E7=82=B9.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typesc?= =?UTF-8?q?ript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0024.两两交换链表中的节点.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0024.两两交换链表中的节点.md b/problems/0024.两两交换链表中的节点.md index 01abc7b4..bf1fd5e1 100644 --- a/problems/0024.两两交换链表中的节点.md +++ b/problems/0024.两两交换链表中的节点.md @@ -250,6 +250,38 @@ var swapPairs = function (head) { }; ``` +TypeScript: + +```typescript +function swapPairs(head: ListNode | null): ListNode | null { + /** + * 初始状态: + * curNode -> node1 -> node2 -> tmepNode + * 转换过程: + * curNode -> node2 + * curNode -> node2 -> node1 + * curNode -> node2 -> node1 -> tempNode + * curNode = node1 + */ + let retNode: ListNode | null = new ListNode(0, head), + curNode: ListNode | null = retNode, + node1: ListNode | null = null, + node2: ListNode | null = null, + tempNode: ListNode | null = null; + + while (curNode && curNode.next && curNode.next.next) { + node1 = curNode.next; + node2 = curNode.next.next; + tempNode = node2.next; + curNode.next = node2; + node2.next = node1; + node1.next = tempNode; + curNode = node1; + } + return retNode.next; +}; +``` + Kotlin: ```kotlin From bc710f263f477d633798c9b357ce7f4178baddbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E5=AE=A2=E5=AD=A6=E4=BC=9F?= Date: Tue, 11 Jan 2022 14:45:40 +0800 Subject: [PATCH 06/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20104.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6?= =?UTF-8?q?=20Swift=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0104.二叉树的最大深度.md | 77 +++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md index 85b41548..7038598b 100644 --- a/problems/0104.二叉树的最大深度.md +++ b/problems/0104.二叉树的最大深度.md @@ -653,5 +653,82 @@ int maxDepth(struct TreeNode* root){ } ``` +## Swift + +>二叉树最大深度 +```swift +// 递归 - 后序 +func maxDepth1(_ root: TreeNode?) -> Int { + return _maxDepth1(root) +} +func _maxDepth1(_ root: TreeNode?) -> Int { + if root == nil { + return 0 + } + let leftDepth = _maxDepth1(root!.left) + let rightDepth = _maxDepth1(root!.right) + return 1 + max(leftDepth, rightDepth) +} + +// 层序 +func maxDepth(_ root: TreeNode?) -> Int { + guard let root = root else { + return 0 + } + var queue = [TreeNode]() + queue.append(root) + var res: Int = 0 + while !queue.isEmpty { + res += 1 + for _ in 0 ..< queue.count { + let node = queue.removeFirst() + if let left = node.left { + queue.append(left) + } + if let right = node.right { + queue.append(right) + } + } + } + return res +} +``` + +>N叉树最大深度 +```swift +// 递归 +func maxDepth(_ root: Node?) -> Int { + guard let root = root else { + return 0 + } + var depth = 0 + for node in root.children { + depth = max(depth, maxDepth(node)) + } + return depth + 1 +} + +// 迭代-层序遍历 +func maxDepth1(_ root: Node?) -> Int { + guard let root = root else { + return 0 + } + var depth = 0 + var queue = [Node]() + queue.append(root) + while !queue.isEmpty { + let size = queue.count + depth += 1 + for _ in 0 ..< size { + let node = queue.removeFirst() + for child in node.children { + queue.append(child) + } + } + } + return depth +} +``` + -----------------------
From 0d2aacb400b106a0c1b3b72c769c7920951f5d05 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Tue, 11 Jan 2022 18:58:28 +0800 Subject: [PATCH 07/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880019.=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E9=93=BE=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=AC?= =?UTF-8?q?N=E4=B8=AA=E8=8A=82=E7=82=B9.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0019.删除链表的倒数第N个节点.md | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0019.删除链表的倒数第N个节点.md b/problems/0019.删除链表的倒数第N个节点.md index e041937f..87ee6b12 100644 --- a/problems/0019.删除链表的倒数第N个节点.md +++ b/problems/0019.删除链表的倒数第N个节点.md @@ -181,7 +181,27 @@ var removeNthFromEnd = function(head, n) { return ret.next; }; ``` +TypeScript: + +```typescript +function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null { + let newHead: ListNode | null = new ListNode(0, head); + let slowNode: ListNode | null = newHead, + fastNode: ListNode | null = newHead; + for (let i = 0; i < n; i++) { + fastNode = fastNode.next; + } + while (fastNode.next) { + fastNode = fastNode.next; + slowNode = slowNode.next; + } + slowNode.next = slowNode.next.next; + return newHead.next; +}; +``` + Kotlin: + ```Kotlin fun removeNthFromEnd(head: ListNode?, n: Int): ListNode? { val pre = ListNode(0).apply { From 5b0b3daf2e65f88de0566aef47d74fa1a8b6aef2 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Tue, 11 Jan 2022 19:02:49 +0800 Subject: [PATCH 08/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A0(0019.=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E9=93=BE=E8=A1=A8=E7=9A=84=E5=80=92=E6=95=B0=E7=AC=AC?= =?UTF-8?q?N=E4=B8=AA=E8=8A=82=E7=82=B9.md)=EF=BC=9Atypescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=A2=9E=E5=8A=A0=E6=96=B0=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0019.删除链表的倒数第N个节点.md | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/problems/0019.删除链表的倒数第N个节点.md b/problems/0019.删除链表的倒数第N个节点.md index 87ee6b12..813e9b02 100644 --- a/problems/0019.删除链表的倒数第N个节点.md +++ b/problems/0019.删除链表的倒数第N个节点.md @@ -183,6 +183,8 @@ var removeNthFromEnd = function(head, n) { ``` TypeScript: +版本一(快慢指针法): + ```typescript function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null { let newHead: ListNode | null = new ListNode(0, head); @@ -200,6 +202,48 @@ function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null { }; ``` +版本二(计算节点总数法): + +```typescript +function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null { + let curNode: ListNode | null = head; + let listSize: number = 0; + while (curNode) { + curNode = curNode.next; + listSize++; + } + if (listSize === n) { + head = head.next; + } else { + curNode = head; + for (let i = 0; i < listSize - n - 1; i++) { + curNode = curNode.next; + } + curNode.next = curNode.next.next; + } + return head; +}; +``` + +版本三(递归倒退n法): + +```typescript +function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null { + let newHead: ListNode | null = new ListNode(0, head); + let cnt = 0; + function recur(node) { + if (node === null) return; + recur(node.next); + cnt++; + if (cnt === n + 1) { + node.next = node.next.next; + } + } + recur(newHead); + return newHead.next; +}; +``` + Kotlin: ```Kotlin From 4a611bbdd9884424fad04710e380f6549310cdd4 Mon Sep 17 00:00:00 2001 From: leeeeeeewii <54872662+leeeeeeewii@users.noreply.github.com> Date: Tue, 11 Jan 2022 19:07:15 +0800 Subject: [PATCH 09/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=2034.=20=E5=9C=A8?= =?UTF-8?q?=E6=8E=92=E5=BA=8F=E6=95=B0=E7=BB=84=E4=B8=AD=E6=9F=A5=E6=89=BE?= =?UTF-8?q?=E5=85=83=E7=B4=A0=E7=9A=84=E7=AC=AC=E4=B8=80=E4=B8=AA=E5=92=8C?= =?UTF-8?q?=E6=9C=80=E5=90=8E=E4=B8=80=E4=B8=AA=E4=BD=8D=E7=BD=AE=20Go?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...元素的第一个和最后一个位置.md | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md index 2b96e41b..2ae8186a 100644 --- a/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md +++ b/problems/0034.在排序数组中查找元素的第一个和最后一个位置.md @@ -389,6 +389,50 @@ class Solution: ### Go ```go +func searchRange(nums []int, target int) []int { + leftBorder := getLeft(nums, target) + rightBorder := getRight(nums, target) + // 情况一 + if leftBorder == -2 || rightBorder == -2 { + return []int{-1, -1} + } + // 情况三 + if rightBorder - leftBorder > 1 { + return []int{leftBorder + 1, rightBorder - 1} + } + // 情况二 + return []int{-1, -1} +} + +func getLeft(nums []int, target int) int { + left, right := 0, len(nums)-1 + border := -2 // 记录border没有被赋值的情况;这里不能赋值-1,target = num[0]时,会无法区分情况一和情况二 + for left <= right { // []闭区间 + mid := left + ((right - left) >> 1) + if nums[mid] >= target { // 找到第一个等于target的位置 + right = mid - 1 + border = right + } else { + left = mid + 1 + } + } + return border +} + +func getRight(nums []int, target int) int { + left, right := 0, len(nums) - 1 + border := -2 + for left <= right { + mid := left + ((right - left) >> 1) + if nums[mid] > target { + right = mid - 1 + } else { // 找到第一个大于target的位置 + left = mid + 1 + border = left + } + } + return border +} ``` ### JavaScript From a30d23871841797548b87ab1cc09ceda7be19bca Mon Sep 17 00:00:00 2001 From: leeeeeeewii <54872662+leeeeeeewii@users.noreply.github.com> Date: Wed, 12 Jan 2022 00:29:24 +0800 Subject: [PATCH 10/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A00283.=E7=A7=BB=E5=8A=A8?= =?UTF-8?q?=E9=9B=B6=20python=E4=B8=8D=E5=90=8C=E6=96=B9=E6=B3=95=EF=BC=8C?= =?UTF-8?q?Go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0283.移动零.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0283.移动零.md b/problems/0283.移动零.md index 13b9c26c..bb75a696 100644 --- a/problems/0283.移动零.md +++ b/problems/0283.移动零.md @@ -89,9 +89,33 @@ Python: for i in range(slow, len(nums)): nums[i] = 0 ``` +交换前后变量,避免补零 +```python + def moveZeroes(self, nums: List[int]) -> None: + slow, fast = 0, 0 + while fast < len(nums): + if nums[fast] != 0: + nums[slow], nums[fast] = nums[fast], nums[slow] + slow += 1 # 保持[0, slow)区间是没有0的 + fast += 1 +``` Go: +```go +func moveZeroes(nums []int) { + slow := 0 + for fast := 0; fast < len(nums); fast ++ { + if nums[fast] != 0 { + temp := nums[slow] + nums[slow] = nums[fast] + nums[fast] = temp + slow++ + } + } +} +``` + JavaScript: ```javascript var moveZeroes = function(nums) { From c93867f0700e455615ca31b564253e1a8f00c603 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E5=AE=A2=E5=AD=A6=E4=BC=9F?= Date: Wed, 12 Jan 2022 12:54:12 +0800 Subject: [PATCH 11/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20111.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6?= =?UTF-8?q?=20Swift=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0111.二叉树的最小深度.md | 43 +++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/problems/0111.二叉树的最小深度.md b/problems/0111.二叉树的最小深度.md index fc93d918..a439322a 100644 --- a/problems/0111.二叉树的最小深度.md +++ b/problems/0111.二叉树的最小深度.md @@ -404,8 +404,51 @@ var minDepth = function(root) { }; ``` +## Swift +> 递归 +```Swift +func minDepth(_ root: TreeNode?) -> Int { + guard let root = root else { + return 0 + } + if root.left == nil && root.right != nil { + return 1 + minDepth(root.right) + } + if root.left != nil && root.right == nil { + return 1 + minDepth(root.left) + } + return 1 + min(minDepth(root.left), minDepth(root.right)) +} +``` +> 迭代 +```Swift +func minDepth(_ root: TreeNode?) -> Int { + guard let root = root else { + return 0 + } + var res = 0 + var queue = [TreeNode]() + queue.append(root) + while !queue.isEmpty { + res += 1 + for _ in 0 ..< queue.count { + let node = queue.removeFirst() + if node.left == nil && node.right == nil { + return res + } + if let left = node.left { + queue.append(left) + } + if let right = node.right { + queue.append(right) + } + } + } + return res +} +``` -----------------------
From ba8e46f241effbb7916bbe777d51c35ad529b4f1 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Wed, 12 Jan 2022 14:32:40 +0800 Subject: [PATCH 12/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=88=E9=9D=A2?= =?UTF-8?q?=E8=AF=95=E9=A2=9802.07.=E9=93=BE=E8=A1=A8=E7=9B=B8=E4=BA=A4.md?= =?UTF-8?q?=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/面试题02.07.链表相交.md | 37 +++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/problems/面试题02.07.链表相交.md b/problems/面试题02.07.链表相交.md index e8198aa0..dd91f069 100644 --- a/problems/面试题02.07.链表相交.md +++ b/problems/面试题02.07.链表相交.md @@ -239,6 +239,43 @@ var getIntersectionNode = function(headA, headB) { }; ``` +TypeScript: + +```typescript +function getIntersectionNode(headA: ListNode | null, headB: ListNode | null): ListNode | null { + let sizeA: number = 0, + sizeB: number = 0; + let curA: ListNode | null = headA, + curB: ListNode | null = headB; + while (curA) { + sizeA++; + curA = curA.next; + } + while (curB) { + sizeB++; + curB = curB.next; + } + curA = headA; + curB = headB; + if (sizeA < sizeB) { + [sizeA, sizeB] = [sizeB, sizeA]; + [curA, curB] = [curB, curA]; + } + let gap = sizeA - sizeB; + while (gap-- && curA) { + curA = curA.next; + } + while (curA && curB) { + if (curA === curB) { + return curA; + } + curA = curA.next; + curB = curB.next; + } + return null; +}; +``` + C: ```c From e2ee3ea4501f75818d346b4613a4c3ab27c754ab Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Wed, 12 Jan 2022 16:25:53 +0800 Subject: [PATCH 13/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880142.=E7=8E=AF?= =?UTF-8?q?=E5=BD=A2=E9=93=BE=E8=A1=A8II.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0142.环形链表II.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/0142.环形链表II.md b/problems/0142.环形链表II.md index dfe042b8..e8ca950d 100644 --- a/problems/0142.环形链表II.md +++ b/problems/0142.环形链表II.md @@ -294,7 +294,30 @@ var detectCycle = function(head) { }; ``` +TypeScript: + +```typescript +function detectCycle(head: ListNode | null): ListNode | null { + let slowNode: ListNode | null = head, + fastNode: ListNode | null = head; + while (fastNode !== null && fastNode.next !== null) { + slowNode = (slowNode as ListNode).next; + fastNode = fastNode.next.next; + if (slowNode === fastNode) { + slowNode = head; + while (slowNode !== fastNode) { + slowNode = (slowNode as ListNode).next; + fastNode = (fastNode as ListNode).next; + } + return slowNode; + } + } + return null; +}; +``` + Swift: + ```swift class Solution { func detectCycle(_ head: ListNode?) -> ListNode? { From 21fc5cd17bba8d75d22a56a832c19958610b28b2 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Wed, 12 Jan 2022 18:21:08 +0800 Subject: [PATCH 14/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880242.=E6=9C=89?= =?UTF-8?q?=E6=95=88=E7=9A=84=E5=AD=97=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8D?= =?UTF-8?q?.md=EF=BC=89=EF=BC=9A=E5=A2=9E=E5=8A=A0typescript=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0242.有效的字母异位词.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index cdcf39ce..52f8e667 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -214,7 +214,23 @@ var isAnagram = function(s, t) { }; ``` +TypeScript: + +```typescript +function isAnagram(s: string, t: string): boolean { + if (s.length !== t.length) return false; + let helperArr: number[] = new Array(26).fill(0); + let pivot: number = 'a'.charCodeAt(0); + for (let i = 0, length = s.length; i < length; i++) { + helperArr[s.charCodeAt(i) - pivot]++; + helperArr[t.charCodeAt(i) - pivot]--; + } + return helperArr.every(i => i === 0); +}; +``` + Swift: + ```Swift func isAnagram(_ s: String, _ t: String) -> Bool { if s.count != t.count { From 014dbf0f79d09fcf46981923f62cf2e879961905 Mon Sep 17 00:00:00 2001 From: Guanzhong Pan Date: Wed, 12 Jan 2022 15:44:55 +0000 Subject: [PATCH 15/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200509.=E6=96=90?= =?UTF-8?q?=E6=B3=A2=E9=82=A3=E5=A5=91=E6=95=B0.md=20C=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0509.斐波那契数.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/0509.斐波那契数.md b/problems/0509.斐波那契数.md index d8020d83..c6ce76c0 100644 --- a/problems/0509.斐波那契数.md +++ b/problems/0509.斐波那契数.md @@ -245,6 +245,38 @@ var fib = function(n) { }; ``` +### C +动态规划: +```c +int fib(int n){ + //当n <= 1时,返回n + if(n <= 1) + return n; + //动态开辟一个int数组,大小为n+1 + int *dp = (int *)malloc(sizeof(int) * (n + 1)); + //设置0号位为0,1号为为1 + dp[0] = 0; + dp[1] = 1; + + //从前向后遍历数组(i=2; i <= n; ++i),下标为n时的元素为dp[i-1] + dp[i-2] + int i; + for(i = 2; i <= n; ++i) { + dp[i] = dp[i - 1] + dp[i - 2]; + } + return dp[n]; +} +``` + +递归实现: +```c +int fib(int n){ + //若n小于等于1,返回n + if(n <= 1) + return n; + //否则返回fib(n-1) + fib(n-2) + return fib(n-1) + fib(n-2); +} +``` ----------------------- From 165fd427339ced15bc7b4e812d5c18f52638cb2f Mon Sep 17 00:00:00 2001 From: Guanzhong Pan Date: Wed, 12 Jan 2022 15:59:27 +0000 Subject: [PATCH 16/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200070.=E7=88=AC?= =?UTF-8?q?=E6=A5=BC=E6=A2=AF.md=20C=E8=AF=AD=E8=A8=80=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0070.爬楼梯.md | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/problems/0070.爬楼梯.md b/problems/0070.爬楼梯.md index 97db670a..7f47a991 100644 --- a/problems/0070.爬楼梯.md +++ b/problems/0070.爬楼梯.md @@ -296,6 +296,48 @@ var climbStairs = function(n) { }; ``` +### C +```c +int climbStairs(int n){ + //若n<=2,返回n + if(n <= 2) + return n; + //初始化dp数组,数组大小为n+1 + int *dp = (int *)malloc(sizeof(int) * (n + 1)); + dp[0] = 0, dp[1] = 1, dp[2] = 2; + + //从前向后遍历数组,dp[i] = dp[i-1] + dp[i-2] + int i; + for(i = 3; i <= n; ++i) { + dp[i] = dp[i - 1] + dp[i - 2]; + } + //返回dp[n] + return dp[n]; +} +``` + +优化空间复杂度: +```c +int climbStairs(int n){ + //若n<=2,返回n + if(n <= 2) + return n; + //初始化dp数组,数组大小为3 + int *dp = (int *)malloc(sizeof(int) * 3); + dp[1] = 1, dp[2] = 2; + + //只记录前面两个台阶的状态 + int i; + for(i = 3; i <= n; ++i) { + int sum = dp[1] + dp[2]; + dp[1] = dp[2]; + dp[2] = sum; + } + //返回dp[2] + return dp[2]; +} +``` + -----------------------
From 518b48dbb407b706dc4a0296bf8b3621e52fdf27 Mon Sep 17 00:00:00 2001 From: Guanzhong Pan Date: Wed, 12 Jan 2022 16:19:37 +0000 Subject: [PATCH 17/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200746.=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E6=9C=80=E5=B0=8F=E8=8A=B1=E8=B4=B9=E7=88=AC=E6=A5=BC?= =?UTF-8?q?=E6=A2=AF.md=20C=E8=AF=AD=E8=A8=80=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0746.使用最小花费爬楼梯.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/problems/0746.使用最小花费爬楼梯.md b/problems/0746.使用最小花费爬楼梯.md index a146cc8b..91cd2c8b 100644 --- a/problems/0746.使用最小花费爬楼梯.md +++ b/problems/0746.使用最小花费爬楼梯.md @@ -266,5 +266,21 @@ var minCostClimbingStairs = function(cost) { }; ``` +### C +```c +int minCostClimbingStairs(int* cost, int costSize){ + //开辟dp数组,大小为costSize + int *dp = (int *)malloc(sizeof(int) * costSize); + //初始化dp[0] = cost[0], dp[1] = cost[1] + dp[0] = cost[0], dp[1] = cost[1]; + + int i; + for(i = 2; i < costSize; ++i) { + dp[i] = (dp[i-1] < dp[i-2] ? dp[i-1] : dp[i-2]) + cost[i]; + } + //选出倒数2层楼梯中较小的 + return dp[i-1] < dp[i-2] ? dp[i-1] : dp[i-2]; +} +``` -----------------------
From c6c39724089a07c5cdd809cca27dd46047fc998f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E5=AE=A2=E5=AD=A6=E4=BC=9F?= Date: Thu, 13 Jan 2022 14:08:26 +0800 Subject: [PATCH 18/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20222.=E5=AE=8C?= =?UTF-8?q?=E5=85=A8=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E4=B8=AA=E6=95=B0=20Swift=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0222.完全二叉树的节点个数.md | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/problems/0222.完全二叉树的节点个数.md b/problems/0222.完全二叉树的节点个数.md index 754a6094..8d38bace 100644 --- a/problems/0222.完全二叉树的节点个数.md +++ b/problems/0222.完全二叉树的节点个数.md @@ -522,5 +522,73 @@ int countNodes(struct TreeNode* root){ } ``` +## Swift: + +> 递归 +```swift +func countNodes(_ root: TreeNode?) -> Int { + return _countNodes(root) +} +func _countNodes(_ root: TreeNode?) -> Int { + guard let root = root else { + return 0 + } + let leftCount = _countNodes(root.left) + let rightCount = _countNodes(root.right) + return 1 + leftCount + rightCount +} +``` + +> 层序遍历 +```Swift +func countNodes(_ root: TreeNode?) -> Int { + guard let root = root else { + return 0 + } + var res = 0 + var queue = [TreeNode]() + queue.append(root) + while !queue.isEmpty { + let size = queue.count + for _ in 0 ..< size { + let node = queue.removeFirst() + res += 1 + if let left = node.left { + queue.append(left) + } + if let right = node.right { + queue.append(right) + } + } + } + return res +} +``` + +> 利用完全二叉树性质 +```Swift +func countNodes(_ root: TreeNode?) -> Int { + guard let root = root else { + return 0 + } + var leftNode = root.left + var rightNode = root.right + var leftDepth = 0 + var rightDepth = 0 + while leftNode != nil { + leftNode = leftNode!.left + leftDepth += 1 + } + while rightNode != nil { + rightNode = rightNode!.right + rightDepth += 1 + } + if leftDepth == rightDepth { + return (2 << leftDepth) - 1 + } + return countNodes(root.left) + countNodes(root.right) + 1 +} +``` + -----------------------
From 3d36ecc092325ce614404d5cc1b69337153d1187 Mon Sep 17 00:00:00 2001 From: YiChih Wang Date: Thu, 13 Jan 2022 21:55:14 +0800 Subject: [PATCH 19/21] =?UTF-8?q?0452.=E7=94=A8=E6=9C=80=E5=B0=91=E6=95=B0?= =?UTF-8?q?=E9=87=8F=E7=9A=84=E7=AE=AD=E5=BC=95=E7=88=86=E6=B0=94=E7=90=83?= =?UTF-8?q?=20Rust=20=E8=AA=9E=E8=A8=80=E5=AF=A6=E7=8F=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0452.用最少数量的箭引爆气球.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0452.用最少数量的箭引爆气球.md b/problems/0452.用最少数量的箭引爆气球.md index ebfe648f..33bbad55 100644 --- a/problems/0452.用最少数量的箭引爆气球.md +++ b/problems/0452.用最少数量的箭引爆气球.md @@ -239,5 +239,30 @@ int findMinArrowShots(int** points, int pointsSize, int* pointsColSize){ } ``` +### Rust +```Rust +use std::cmp; +impl Solution { + pub fn find_min_arrow_shots(mut points: Vec>) -> i32 { + if points.is_empty() { + return 0; + } + points.sort_by_key(|point| point[0]); + + let size = points.len(); + let mut count = 1; + + for i in 1..size { + if points[i][0] > points[i-1][1] { + count += 1; + } else { + points[i][1] = cmp::min(points[i][1], points[i-1][1]); + } + } + + return count; + } +} +``` -----------------------
From 0c184a5ee6e3f52d32e23a0100a5215512f8517b Mon Sep 17 00:00:00 2001 From: Guanzhong Pan Date: Thu, 13 Jan 2022 14:04:08 +0000 Subject: [PATCH 20/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200062.=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E8=B7=AF=E5=BE=84.md=20C=E8=AF=AD=E8=A8=80=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0062.不同路径.md | 36 +++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/problems/0062.不同路径.md b/problems/0062.不同路径.md index e209abb1..efa85a03 100644 --- a/problems/0062.不同路径.md +++ b/problems/0062.不同路径.md @@ -347,6 +347,42 @@ var uniquePaths = function(m, n) { }; ``` +### C +```c +//初始化dp数组 +int **initDP(int m, int n) { + //动态开辟dp数组 + int **dp = (int**)malloc(sizeof(int *) * m); + int i, j; + for(i = 0; i < m; ++i) { + dp[i] = (int *)malloc(sizeof(int) * n); + } + + //从0,0到i,0只有一种走法,所以dp[i][0]都是1,同理dp[0][j]也是1 + for(i = 0; i < m; ++i) + dp[i][0] = 1; + for(j = 0; j < n; ++j) + dp[0][j] = 1; + return dp; +} + +int uniquePaths(int m, int n){ + //dp数组,dp[i][j]代表从dp[0][0]到dp[i][j]有几种走法 + int **dp = initDP(m, n); + + int i, j; + //到达dp[i][j]只能从dp[i-1][j]和dp[i][j-1]出发 + //dp[i][j] = dp[i-1][j] + dp[i][j-1] + for(i = 1; i < m; ++i) { + for(j = 1; j < n; ++j) { + dp[i][j] = dp[i-1][j] + dp[i][j-1]; + } + } + int result = dp[m-1][n-1]; + free(dp); + return result; +} +``` -----------------------
From 4f4a54fa7928a11d33a498310426793daeabab77 Mon Sep 17 00:00:00 2001 From: ArthurP Date: Sun, 16 Jan 2022 09:50:17 +0000 Subject: [PATCH 21/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200063.=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E8=B7=AF=E5=BE=84II.md=20C=E8=AF=AD=E8=A8=80=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0063.不同路径II.md | 54 +++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/problems/0063.不同路径II.md b/problems/0063.不同路径II.md index 490b6b5c..6f405d6a 100644 --- a/problems/0063.不同路径II.md +++ b/problems/0063.不同路径II.md @@ -333,6 +333,60 @@ var uniquePathsWithObstacles = function(obstacleGrid) { }; ``` +C +```c +//初始化dp数组 +int **initDP(int m, int n, int** obstacleGrid) { + int **dp = (int**)malloc(sizeof(int*) * m); + int i, j; + //初始化每一行数组 + for(i = 0; i < m; ++i) { + dp[i] = (int*)malloc(sizeof(int) * n); + } + + //先将第一行第一列设为0 + for(i = 0; i < m; ++i) { + dp[i][0] = 0; + } + for(j = 0; j < n; ++j) { + dp[0][j] = 0; + } + + //若碰到障碍,之后的都走不了。退出循环 + for(i = 0; i < m; ++i) { + if(obstacleGrid[i][0]) { + break; + } + dp[i][0] = 1; + } + for(j = 0; j < n; ++j) { + if(obstacleGrid[0][j]) + break; + dp[0][j] = 1; + } + return dp; +} + +int uniquePathsWithObstacles(int** obstacleGrid, int obstacleGridSize, int* obstacleGridColSize){ + int m = obstacleGridSize, n = *obstacleGridColSize; + //初始化dp数组 + int **dp = initDP(m, n, obstacleGrid); + + int i, j; + for(i = 1; i < m; ++i) { + for(j = 1; j < n; ++j) { + //若当前i,j位置有障碍 + if(obstacleGrid[i][j]) + //路线不同 + dp[i][j] = 0; + else + dp[i][j] = dp[i-1][j] + dp[i][j-1]; + } + } + //返回最后终点的路径个数 + return dp[m-1][n-1]; +} +``` -----------------------