From 8614090773bf77964b350a31afac82752fd59234 Mon Sep 17 00:00:00 2001 From: jxxiao Date: Fri, 10 Dec 2021 21:46:15 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=200077.=E7=BB=84?= =?UTF-8?q?=E5=90=88=20python=E6=9C=AA=E5=89=AA=E6=9E=9D=E5=92=8C=E5=89=AA?= =?UTF-8?q?=E6=9E=9D=E4=BB=A3=E7=A0=81=E4=B8=80=E6=A0=B7=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0077.组合.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/problems/0077.组合.md b/problems/0077.组合.md index 16313cb8..edd9f393 100644 --- a/problems/0077.组合.md +++ b/problems/0077.组合.md @@ -405,7 +405,7 @@ class Solution: if len(path) == k: res.append(path[:]) return - for i in range(StartIndex, n-(k-len(path)) + 2): + for i in range(StartIndex, n + 1): path.append(i) backtrack(n, k, i+1) path.pop() @@ -414,7 +414,7 @@ class Solution: ``` 剪枝: -```python3 +```python class Solution: def combine(self, n: int, k: int) -> List[List[int]]: res=[] #存放符合条件结果的集合 @@ -423,7 +423,7 @@ class Solution: if len(path) == k: res.append(path[:]) return - for i in range(startIndex,n-(k-len(path))+2): #优化的地方 + for i in range(startIndex,n - (k - len(path)) + 2): #优化的地方 path.append(i) #处理节点 backtrack(n,k,i+1) #递归 path.pop() #回溯,撤销处理的节点 From a981cee54cad59114335f32bcb88b4a2e39d0342 Mon Sep 17 00:00:00 2001 From: ShrinkLynn Date: Sun, 12 Dec 2021 19:54:33 +0800 Subject: [PATCH 2/3] [ShrinkLynn] 0028 add swift version --- problems/0028.实现strStr.md | 51 +++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/problems/0028.实现strStr.md b/problems/0028.实现strStr.md index 1c654dd4..d6c5ee27 100644 --- a/problems/0028.实现strStr.md +++ b/problems/0028.实现strStr.md @@ -894,7 +894,58 @@ var strStr = function (haystack, needle) { }; ``` +Swift 版本 +> 前缀表统一减一 + +```swift +func strStr(_ haystack: String, _ needle: String) -> Int { + + let s = Array(haystack), p = Array(needle) + guard p.count != 0 else { return 0 } + + // 2 pointer + var j = -1 + var next = [Int](repeating: -1, count: needle.count) + // KMP + getNext(&next, needle: p) + for i in 0 ..< s.count { + while j >= 0 && s[i] != p[j + 1] { + //不匹配之后寻找之前匹配的位置 + j = next[j] + } + if s[i] == p[j + 1] { + //匹配,双指针同时后移 + j += 1 + } + if j == (p.count - 1) { + //出现匹配字符串 + return i - p.count + 1 + } + } + return -1 +} + +//前缀表统一减一 +func getNext(_ next: inout [Int], needle: [Character]) { + + var j: Int = -1 + next[0] = j + + // i 从 1 开始 + for i in 1 ..< needle.count { + while j >= 0 && needle[i] != needle[j + 1] { + j = next[j] + } + if needle[i] == needle[j + 1] { + j += 1; + } + next[i] = j + } + print(next) +} + +``` -----------------------
From 52ba6c2b35639233668d59eef75e41ceaf6d39bf 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, 14 Dec 2021 13:29:25 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E9=80=92=E5=BD=92=E9=81=8D=E5=8E=86=20Swift?= =?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/二叉树的递归遍历.md | 51 ++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/problems/二叉树的递归遍历.md b/problems/二叉树的递归遍历.md index 875e6169..0468b79b 100644 --- a/problems/二叉树的递归遍历.md +++ b/problems/二叉树的递归遍历.md @@ -409,5 +409,56 @@ int* postorderTraversal(struct TreeNode* root, int* returnSize){ } ``` +Swift: +前序遍历:(144.二叉树的前序遍历) +```Swift +func preorderTraversal(_ root: TreeNode?) -> [Int] { + var res = [Int]() + preorder(root, res: &res) + return res +} +func preorder(_ root: TreeNode?, res: inout [Int]) { + if root == nil { + return + } + res.append(root!.val) + preorder(root!.left, res: &res) + preorder(root!.right, res: &res) +} +``` + +中序遍历:(94. 二叉树的中序遍历) +```Swift +func inorderTraversal(_ root: TreeNode?) -> [Int] { + var res = [Int]() + inorder(root, res: &res) + return res +} +func inorder(_ root: TreeNode?, res: inout [Int]) { + if root == nil { + return + } + inorder(root!.left, res: &res) + res.append(root!.val) + inorder(root!.right, res: &res) +} +``` + +后序遍历:(145. 二叉树的后序遍历) +```Swift +func postorderTraversal(_ root: TreeNode?) -> [Int] { + var res = [Int]() + postorder(root, res: &res) + return res +} +func postorder(_ root: TreeNode?, res: inout [Int]) { + if root == nil { + return + } + postorder(root!.left, res: &res) + postorder(root!.right, res: &res) + res.append(root!.val) +} +``` -----------------------