mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 11:34:46 +08:00
@ -37,7 +37,7 @@
|
|||||||
|
|
||||||
## 数字和字母如何映射
|
## 数字和字母如何映射
|
||||||
|
|
||||||
可以使用map或者定义一个二位数组,例如:string letterMap[10],来做映射,我这里定义一个二维数组,代码如下:
|
可以使用map或者定义一个二维数组,例如:string letterMap[10],来做映射,我这里定义一个二维数组,代码如下:
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
const string letterMap[10] = {
|
const string letterMap[10] = {
|
||||||
|
@ -455,7 +455,6 @@ func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] {
|
|||||||
var path = [Int]()
|
var path = [Int]()
|
||||||
func backtracking(sum: Int, startIndex: Int) {
|
func backtracking(sum: Int, startIndex: Int) {
|
||||||
// 终止条件
|
// 终止条件
|
||||||
if sum > target { return }
|
|
||||||
if sum == target {
|
if sum == target {
|
||||||
result.append(path)
|
result.append(path)
|
||||||
return
|
return
|
||||||
@ -464,8 +463,11 @@ func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] {
|
|||||||
let end = candidates.count
|
let end = candidates.count
|
||||||
guard startIndex < end else { return }
|
guard startIndex < end else { return }
|
||||||
for i in startIndex ..< end {
|
for i in startIndex ..< end {
|
||||||
|
let sum = sum + candidates[i] // 使用局部变量隐藏回溯
|
||||||
|
if sum > target { continue } // 剪枝
|
||||||
|
|
||||||
path.append(candidates[i]) // 处理
|
path.append(candidates[i]) // 处理
|
||||||
backtracking(sum: sum + candidates[i], startIndex: i) // sum这里用新变量完成回溯,i不用+1以重复访问
|
backtracking(sum: sum, startIndex: i) // i不用+1以重复访问
|
||||||
path.removeLast() // 回溯
|
path.removeLast() // 回溯
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -627,7 +627,7 @@ int** combine(int n, int k, int* returnSize, int** returnColumnSizes){
|
|||||||
func combine(_ n: Int, _ k: Int) -> [[Int]] {
|
func combine(_ n: Int, _ k: Int) -> [[Int]] {
|
||||||
var path = [Int]()
|
var path = [Int]()
|
||||||
var result = [[Int]]()
|
var result = [[Int]]()
|
||||||
func backtracking(_ n: Int, _ k: Int, _ startIndex: Int) {
|
func backtracking(start: Int) {
|
||||||
// 结束条件,并收集结果
|
// 结束条件,并收集结果
|
||||||
if path.count == k {
|
if path.count == k {
|
||||||
result.append(path)
|
result.append(path)
|
||||||
@ -638,15 +638,15 @@ func combine(_ n: Int, _ k: Int) -> [[Int]] {
|
|||||||
// let end = n
|
// let end = n
|
||||||
// 剪枝优化
|
// 剪枝优化
|
||||||
let end = n - (k - path.count) + 1
|
let end = n - (k - path.count) + 1
|
||||||
guard startIndex <= end else { return }
|
guard start <= end else { return }
|
||||||
for i in startIndex ... end {
|
for i in start ... end {
|
||||||
path.append(i) // 处理结点
|
path.append(i) // 处理结点
|
||||||
backtracking(n, k, i + 1) // 递归
|
backtracking(start: i + 1) // 递归
|
||||||
path.removeLast() // 回溯
|
path.removeLast() // 回溯
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
backtracking(n, k, 1)
|
backtracking(start: 1)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -300,7 +300,7 @@ Swift:
|
|||||||
func combine(_ n: Int, _ k: Int) -> [[Int]] {
|
func combine(_ n: Int, _ k: Int) -> [[Int]] {
|
||||||
var path = [Int]()
|
var path = [Int]()
|
||||||
var result = [[Int]]()
|
var result = [[Int]]()
|
||||||
func backtracking(_ n: Int, _ k: Int, _ startIndex: Int) {
|
func backtracking(start: Int) {
|
||||||
// 结束条件,并收集结果
|
// 结束条件,并收集结果
|
||||||
if path.count == k {
|
if path.count == k {
|
||||||
result.append(path)
|
result.append(path)
|
||||||
@ -311,15 +311,15 @@ func combine(_ n: Int, _ k: Int) -> [[Int]] {
|
|||||||
// let end = n
|
// let end = n
|
||||||
// 剪枝优化
|
// 剪枝优化
|
||||||
let end = n - (k - path.count) + 1
|
let end = n - (k - path.count) + 1
|
||||||
guard startIndex <= end else { return }
|
guard start <= end else { return }
|
||||||
for i in startIndex ... end {
|
for i in start ... end {
|
||||||
path.append(i) // 处理结点
|
path.append(i) // 处理结点
|
||||||
backtracking(n, k, i + 1) // 递归
|
backtracking(start: i + 1) // 递归
|
||||||
path.removeLast() // 回溯
|
path.removeLast() // 回溯
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
backtracking(n, k, 1)
|
backtracking(start: 1)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -575,12 +575,9 @@ func partition(_ s: String) -> [[String]] {
|
|||||||
|
|
||||||
for i in startIndex ..< s.count {
|
for i in startIndex ..< s.count {
|
||||||
// 回文则收集,否则跳过
|
// 回文则收集,否则跳过
|
||||||
if isPalindrome(start: startIndex, end: i) {
|
guard isPalindrome(start: startIndex, end: i) else { continue }
|
||||||
let substring = String(s[startIndex ... i])
|
let substring = String(s[startIndex ... i])
|
||||||
path.append(substring)
|
path.append(substring) // 处理
|
||||||
} else {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
backtracking(startIndex: i + 1) // 寻找下一个起始位置的子串
|
backtracking(startIndex: i + 1) // 寻找下一个起始位置的子串
|
||||||
if !path.isEmpty { path.removeLast() } // 回溯
|
if !path.isEmpty { path.removeLast() } // 回溯
|
||||||
}
|
}
|
||||||
|
@ -462,7 +462,7 @@ int** combinationSum3(int k, int n, int* returnSize, int** returnColumnSizes){
|
|||||||
func combinationSum3(_ count: Int, _ targetSum: Int) -> [[Int]] {
|
func combinationSum3(_ count: Int, _ targetSum: Int) -> [[Int]] {
|
||||||
var result = [[Int]]()
|
var result = [[Int]]()
|
||||||
var path = [Int]()
|
var path = [Int]()
|
||||||
func backtracking(sum: Int, startIndex: Int) {
|
func backtracking(sum: Int, start: Int) {
|
||||||
// 剪枝
|
// 剪枝
|
||||||
if sum > targetSum { return }
|
if sum > targetSum { return }
|
||||||
// 终止条件
|
// 终止条件
|
||||||
@ -474,16 +474,16 @@ func combinationSum3(_ count: Int, _ targetSum: Int) -> [[Int]] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 单层逻辑
|
// 单层逻辑
|
||||||
let endIndex = 9
|
let end = 9
|
||||||
guard startIndex <= endIndex else { return }
|
guard start <= end else { return }
|
||||||
for i in startIndex ... endIndex {
|
for i in start ... end {
|
||||||
path.append(i) // 处理
|
path.append(i) // 处理
|
||||||
backtracking(sum: sum + i, startIndex: i + 1)
|
backtracking(sum: sum + i, start: i + 1)
|
||||||
path.removeLast() // 回溯
|
path.removeLast() // 回溯
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
backtracking(sum: 0, startIndex: 1)
|
backtracking(sum: 0, start: 1)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -331,7 +331,7 @@ used数组可是全局变量,每层与每层之间公用一个used数组,所
|
|||||||
|
|
||||||
在[回溯算法:N皇后问题](https://programmercarl.com/0051.N皇后.html)中终于迎来了传说中的N皇后。
|
在[回溯算法:N皇后问题](https://programmercarl.com/0051.N皇后.html)中终于迎来了传说中的N皇后。
|
||||||
|
|
||||||
下面我用一个3 * 3 的棋牌,将搜索过程抽象为一颗树,如图:
|
下面我用一个3 * 3 的棋盘,将搜索过程抽象为一颗树,如图:
|
||||||
|
|
||||||

|

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