Merge pull request #960 from bqlin/master

修正回溯部分错别字,优化Swift版本实现
This commit is contained in:
程序员Carl
2021-12-28 16:24:51 +08:00
committed by GitHub
7 changed files with 25 additions and 41 deletions

View File

@ -37,7 +37,7 @@
## 数字和字母如何映射
可以使用map或者定义一个二数组例如string letterMap[10],来做映射,我这里定义一个二维数组,代码如下:
可以使用map或者定义一个二数组例如string letterMap[10],来做映射,我这里定义一个二维数组,代码如下:
```cpp
const string letterMap[10] = {

View File

@ -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() // 回溯
}
}

View File

@ -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
}
```

View File

@ -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
}
```

View File

@ -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() } // 回溯
}

View File

@ -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
}
```

View File

@ -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
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>