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] = { 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() // 回溯 } } 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 } ``` 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() } // 回溯 } 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 } ``` 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 的棋盘,将搜索过程抽象为一颗树,如图:  @@ -437,20 +437,5 @@ N皇后问题分析: **回溯算法系列正式结束,新的系列终将开始,录友们准备开启新的征程!** - -## 其他语言版本 - - -Java: - - -Python: - - -Go: - - - - -----------------------