From 7b7251f1e4bb8125c7f88030a2c93db0c0ceace3 Mon Sep 17 00:00:00 2001 From: bqlin Date: Sat, 18 Dec 2021 13:56:43 +0800 Subject: [PATCH 1/6] =?UTF-8?q?0017.=E7=94=B5=E8=AF=9D=E5=8F=B7=E7=A0=81?= =?UTF-8?q?=E7=9A=84=E5=AD=97=E6=AF=8D=E7=BB=84=E5=90=88=EF=BC=9A=E2=80=9C?= =?UTF-8?q?=E4=BA=8C=E4=BD=8D=E6=95=B0=E7=BB=84=E2=80=9D=20->=20=E2=80=9C?= =?UTF-8?q?=E4=BA=8C=E7=BB=B4=E6=95=B0=E7=BB=84=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0017.电话号码的字母组合.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0017.电话号码的字母组合.md b/problems/0017.电话号码的字母组合.md index 146493e9..7040182f 100644 --- a/problems/0017.电话号码的字母组合.md +++ b/problems/0017.电话号码的字母组合.md @@ -37,7 +37,7 @@ ## 数字和字母如何映射 -可以使用map或者定义一个二位数组,例如:string letterMap[10],来做映射,我这里定义一个二维数组,代码如下: +可以使用map或者定义一个二维数组,例如:string letterMap[10],来做映射,我这里定义一个二维数组,代码如下: ```cpp const string letterMap[10] = { From de0465b773693fa6c19a4da73a008cfa287ce812 Mon Sep 17 00:00:00 2001 From: bqlin Date: Sat, 18 Dec 2021 14:02:02 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E5=9B=9E=E6=BA=AF=E6=80=BB=E7=BB=93?= =?UTF-8?q?=EF=BC=9A=E2=80=9C=E6=A3=8B=E7=89=8C=E2=80=9D=20->=20=E2=80=9C?= =?UTF-8?q?=E6=A3=8B=E7=9B=98=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/回溯总结.md | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/problems/回溯总结.md b/problems/回溯总结.md index 2fefeb04..cff41f24 100644 --- a/problems/回溯总结.md +++ b/problems/回溯总结.md @@ -331,7 +331,7 @@ used数组可是全局变量,每层与每层之间公用一个used数组,所 在[回溯算法:N皇后问题](https://programmercarl.com/0051.N皇后.html)中终于迎来了传说中的N皇后。 -下面我用一个3 * 3 的棋牌,将搜索过程抽象为一颗树,如图: +下面我用一个3 * 3 的棋盘,将搜索过程抽象为一颗树,如图: ![51.N皇后](https://img-blog.csdnimg.cn/20201118225433127.png) @@ -437,20 +437,5 @@ N皇后问题分析: **回溯算法系列正式结束,新的系列终将开始,录友们准备开启新的征程!** - -## 其他语言版本 - - -Java: - - -Python: - - -Go: - - - - -----------------------
From 90aa75c57300eaa7eea9a87d268cb9b0ad8a8553 Mon Sep 17 00:00:00 2001 From: bqlin Date: Sat, 18 Dec 2021 18:45:54 +0800 Subject: [PATCH 3/6] =?UTF-8?q?0077.=E7=BB=84=E5=90=88=E3=80=810077.?= =?UTF-8?q?=E7=BB=84=E5=90=88=E4=BC=98=E5=8C=96=EF=BC=9ASwift=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E5=87=8F=E5=B0=91=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0077.组合.md | 10 +++++----- problems/0077.组合优化.md | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/problems/0077.组合.md b/problems/0077.组合.md index 2bf856df..4ec154e1 100644 --- a/problems/0077.组合.md +++ b/problems/0077.组合.md @@ -627,7 +627,7 @@ int** combine(int n, int k, int* returnSize, int** returnColumnSizes){ func combine(_ n: Int, _ k: Int) -> [[Int]] { var path = [Int]() var result = [[Int]]() - func backtracking(_ n: Int, _ k: Int, _ startIndex: Int) { + func backtracking(start: Int) { // 结束条件,并收集结果 if path.count == k { result.append(path) @@ -638,15 +638,15 @@ func combine(_ n: Int, _ k: Int) -> [[Int]] { // let end = n // 剪枝优化 let end = n - (k - path.count) + 1 - guard startIndex <= end else { return } - for i in startIndex ... end { + guard start <= end else { return } + for i in start ... end { path.append(i) // 处理结点 - backtracking(n, k, i + 1) // 递归 + backtracking(start: i + 1) // 递归 path.removeLast() // 回溯 } } - backtracking(n, k, 1) + backtracking(start: 1) return result } ``` diff --git a/problems/0077.组合优化.md b/problems/0077.组合优化.md index b713bdcd..5fe56e82 100644 --- a/problems/0077.组合优化.md +++ b/problems/0077.组合优化.md @@ -300,7 +300,7 @@ Swift: func combine(_ n: Int, _ k: Int) -> [[Int]] { var path = [Int]() var result = [[Int]]() - func backtracking(_ n: Int, _ k: Int, _ startIndex: Int) { + func backtracking(start: Int) { // 结束条件,并收集结果 if path.count == k { result.append(path) @@ -311,15 +311,15 @@ func combine(_ n: Int, _ k: Int) -> [[Int]] { // let end = n // 剪枝优化 let end = n - (k - path.count) + 1 - guard startIndex <= end else { return } - for i in startIndex ... end { + guard start <= end else { return } + for i in start ... end { path.append(i) // 处理结点 - backtracking(n, k, i + 1) // 递归 + backtracking(start: i + 1) // 递归 path.removeLast() // 回溯 } } - backtracking(n, k, 1) + backtracking(start: 1) return result } ``` From 7c4bafe9b1d4c83a36520e3cc9c37d9c103a3651 Mon Sep 17 00:00:00 2001 From: bqlin Date: Sat, 18 Dec 2021 20:06:04 +0800 Subject: [PATCH 4/6] =?UTF-8?q?0216.=E7=BB=84=E5=90=88=E6=80=BB=E5=92=8CII?= =?UTF-8?q?I=EF=BC=9ASwift=E5=AE=9E=E7=8E=B0=E5=87=8F=E5=B0=91=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0216.组合总和III.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/problems/0216.组合总和III.md b/problems/0216.组合总和III.md index a411117c..26c630b9 100644 --- a/problems/0216.组合总和III.md +++ b/problems/0216.组合总和III.md @@ -462,7 +462,7 @@ int** combinationSum3(int k, int n, int* returnSize, int** returnColumnSizes){ func combinationSum3(_ count: Int, _ targetSum: Int) -> [[Int]] { var result = [[Int]]() var path = [Int]() - func backtracking(sum: Int, startIndex: Int) { + func backtracking(sum: Int, start: Int) { // 剪枝 if sum > targetSum { return } // 终止条件 @@ -474,16 +474,16 @@ func combinationSum3(_ count: Int, _ targetSum: Int) -> [[Int]] { } // 单层逻辑 - let endIndex = 9 - guard startIndex <= endIndex else { return } - for i in startIndex ... endIndex { + let end = 9 + guard start <= end else { return } + for i in start ... end { path.append(i) // 处理 - backtracking(sum: sum + i, startIndex: i + 1) + backtracking(sum: sum + i, start: i + 1) path.removeLast() // 回溯 } } - backtracking(sum: 0, startIndex: 1) + backtracking(sum: 0, start: 1) return result } ``` From 622b443bbe309d91c14693e6b2dff96fd209bbb4 Mon Sep 17 00:00:00 2001 From: bqlin Date: Sat, 18 Dec 2021 20:12:46 +0800 Subject: [PATCH 5/6] =?UTF-8?q?0039.=E7=BB=84=E5=90=88=E6=80=BB=E5=92=8C?= =?UTF-8?q?=EF=BC=9ASwift=E5=AE=9E=E7=8E=B0=E4=BC=98=E5=8C=96=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E5=91=BD=E5=90=8D=EF=BC=8C=E6=B7=BB=E5=8A=A0=E5=89=AA?= =?UTF-8?q?=E6=9E=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0039.组合总和.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/problems/0039.组合总和.md b/problems/0039.组合总和.md index e78f2e2d..0f8fe4f6 100644 --- a/problems/0039.组合总和.md +++ b/problems/0039.组合总和.md @@ -455,7 +455,6 @@ func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] { var path = [Int]() func backtracking(sum: Int, startIndex: Int) { // 终止条件 - if sum > target { return } if sum == target { result.append(path) return @@ -464,8 +463,11 @@ func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] { let end = candidates.count guard startIndex < end else { return } for i in startIndex ..< end { + let sum = sum + candidates[i] // 使用局部变量隐藏回溯 + if sum > target { continue } // 剪枝 + path.append(candidates[i]) // 处理 - backtracking(sum: sum + candidates[i], startIndex: i) // sum这里用新变量完成回溯,i不用+1以重复访问 + backtracking(sum: sum, startIndex: i) // i不用+1以重复访问 path.removeLast() // 回溯 } } From 690897f33f513887c07809bd2fc158004094769c Mon Sep 17 00:00:00 2001 From: bqlin Date: Sat, 18 Dec 2021 20:23:12 +0800 Subject: [PATCH 6/6] =?UTF-8?q?0131.=E5=88=86=E5=89=B2=E5=9B=9E=E6=96=87?= =?UTF-8?q?=E4=B8=B2=EF=BC=9ASwift=E5=AE=9E=E7=8E=B0=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E8=BF=AD=E4=BB=A3=E4=B8=AD=E8=B7=B3=E8=BF=87=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0131.分割回文串.md | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/problems/0131.分割回文串.md b/problems/0131.分割回文串.md index f4d72eea..84a5bd02 100644 --- a/problems/0131.分割回文串.md +++ b/problems/0131.分割回文串.md @@ -575,12 +575,9 @@ func partition(_ s: String) -> [[String]] { for i in startIndex ..< s.count { // 回文则收集,否则跳过 - if isPalindrome(start: startIndex, end: i) { - let substring = String(s[startIndex ... i]) - path.append(substring) - } else { - continue - } + guard isPalindrome(start: startIndex, end: i) else { continue } + let substring = String(s[startIndex ... i]) + path.append(substring) // 处理 backtracking(startIndex: i + 1) // 寻找下一个起始位置的子串 if !path.isEmpty { path.removeLast() } // 回溯 }