This commit is contained in:
krahets
2024-04-07 03:05:15 +08:00
parent aea68142f8
commit d8caf02e9e
21 changed files with 118 additions and 101 deletions

View File

@ -510,7 +510,7 @@ comments: true
path!!.add(root)
if (root.value == 7) {
// 记录解
res!!.add(ArrayList(path!!))
res!!.add(path!!.toMutableList())
}
preOrder(root.left)
preOrder(root.right)
@ -854,7 +854,7 @@ comments: true
path!!.add(root)
if (root.value == 7) {
// 记录解
res!!.add(ArrayList(path!!))
res!!.add(path!!.toMutableList())
}
preOrder(root.left)
preOrder(root.right)
@ -1790,17 +1790,17 @@ comments: true
```kotlin title="preorder_traversal_iii_template.kt"
/* 判断当前状态是否为解 */
fun isSolution(state: List<TreeNode?>): Boolean {
fun isSolution(state: MutableList<TreeNode?>): Boolean {
return state.isNotEmpty() && state[state.size - 1]?.value == 7
}
/* 记录解 */
fun recordSolution(state: MutableList<TreeNode?>?, res: MutableList<List<TreeNode?>?>) {
res.add(state?.let { ArrayList(it) })
fun recordSolution(state: MutableList<TreeNode?>?, res: MutableList<MutableList<TreeNode?>?>) {
res.add(state!!.toMutableList())
}
/* 判断在当前状态下,该选择是否合法 */
fun isValid(state: List<TreeNode?>?, choice: TreeNode?): Boolean {
fun isValid(state: MutableList<TreeNode?>?, choice: TreeNode?): Boolean {
return choice != null && choice.value != 3
}
@ -1817,8 +1817,8 @@ comments: true
/* 回溯算法:例题三 */
fun backtrack(
state: MutableList<TreeNode?>,
choices: List<TreeNode?>,
res: MutableList<List<TreeNode?>?>
choices: MutableList<TreeNode?>,
res: MutableList<MutableList<TreeNode?>?>
) {
// 检查是否为解
if (isSolution(state)) {
@ -1832,7 +1832,7 @@ comments: true
// 尝试:做出选择,更新状态
makeChoice(state, choice)
// 进行下一轮选择
backtrack(state, listOf(choice!!.left, choice.right), res)
backtrack(state, mutableListOf(choice!!.left, choice.right), res)
// 回退:撤销选择,恢复到之前的状态
undoChoice(state, choice)
}

View File

@ -646,17 +646,17 @@ comments: true
fun backtrack(
row: Int,
n: Int,
state: List<MutableList<String>>,
res: MutableList<List<List<String>>?>,
state: MutableList<MutableList<String>>,
res: MutableList<MutableList<MutableList<String>>?>,
cols: BooleanArray,
diags1: BooleanArray,
diags2: BooleanArray
) {
// 当放置完所有行时,记录解
if (row == n) {
val copyState: MutableList<List<String>> = ArrayList()
val copyState = mutableListOf<MutableList<String>>()
for (sRow in state) {
copyState.add(ArrayList(sRow))
copyState.add(sRow.toMutableList())
}
res.add(copyState)
return
@ -685,11 +685,11 @@ comments: true
}
/* 求解 n 皇后 */
fun nQueens(n: Int): List<List<List<String>>?> {
fun nQueens(n: Int): MutableList<MutableList<MutableList<String>>?> {
// 初始化 n*n 大小的棋盘,其中 'Q' 代表皇后,'#' 代表空位
val state: MutableList<MutableList<String>> = ArrayList()
val state = mutableListOf<MutableList<String>>()
for (i in 0..<n) {
val row: MutableList<String> = ArrayList()
val row = mutableListOf<String>()
for (j in 0..<n) {
row.add("#")
}
@ -698,7 +698,7 @@ comments: true
val cols = BooleanArray(n) // 记录列是否有皇后
val diags1 = BooleanArray(2 * n - 1) // 记录主对角线上是否有皇后
val diags2 = BooleanArray(2 * n - 1) // 记录次对角线上是否有皇后
val res: MutableList<List<List<String>>?> = ArrayList()
val res = mutableListOf<MutableList<MutableList<String>>?>()
backtrack(0, n, state, res, cols, diags1, diags2)

View File

@ -471,11 +471,11 @@ comments: true
state: MutableList<Int>,
choices: IntArray,
selected: BooleanArray,
res: MutableList<List<Int>?>
res: MutableList<MutableList<Int>?>
) {
// 当状态长度等于元素数量时,记录解
if (state.size == choices.size) {
res.add(ArrayList(state))
res.add(state.toMutableList())
return
}
// 遍历所有选择
@ -496,9 +496,9 @@ comments: true
}
/* 全排列 I */
fun permutationsI(nums: IntArray): List<List<Int>?> {
val res: MutableList<List<Int>?> = ArrayList()
backtrack(ArrayList(), nums, BooleanArray(nums.size), res)
fun permutationsI(nums: IntArray): MutableList<MutableList<Int>?> {
val res = mutableListOf<MutableList<Int>?>()
backtrack(mutableListOf(), nums, BooleanArray(nums.size), res)
return res
}
```
@ -999,11 +999,11 @@ comments: true
) {
// 当状态长度等于元素数量时,记录解
if (state.size == choices.size) {
res.add(ArrayList(state))
res.add(state.toMutableList())
return
}
// 遍历所有选择
val duplicated: MutableSet<Int> = HashSet()
val duplicated = HashSet<Int>()
for (i in choices.indices) {
val choice = choices[i]
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
@ -1023,8 +1023,8 @@ comments: true
/* 全排列 II */
fun permutationsII(nums: IntArray): MutableList<MutableList<Int>?> {
val res: MutableList<MutableList<Int>?> = ArrayList()
backtrack(ArrayList(), nums, BooleanArray(nums.size), res)
val res = mutableListOf<MutableList<Int>?>()
backtrack(mutableListOf(), nums, BooleanArray(nums.size), res)
return res
}
```

View File

@ -435,11 +435,11 @@ comments: true
target: Int,
total: Int,
choices: IntArray,
res: MutableList<List<Int>?>
res: MutableList<MutableList<Int>?>
) {
// 子集和等于 target 时,记录解
if (total == target) {
res.add(ArrayList(state))
res.add(state.toMutableList())
return
}
// 遍历所有选择
@ -458,10 +458,10 @@ comments: true
}
/* 求解子集和 I包含重复子集 */
fun subsetSumINaive(nums: IntArray, target: Int): List<List<Int>?> {
val state: MutableList<Int> = ArrayList() // 状态(子集)
fun subsetSumINaive(nums: IntArray, target: Int): MutableList<MutableList<Int>?> {
val state = mutableListOf<Int>() // 状态(子集)
val total = 0 // 子集和
val res: MutableList<List<Int>?> = ArrayList() // 结果列表(子集列表)
val res = mutableListOf<MutableList<Int>?>() // 结果列表(子集列表)
backtrack(state, target, total, nums, res)
return res
}
@ -973,11 +973,11 @@ comments: true
target: Int,
choices: IntArray,
start: Int,
res: MutableList<List<Int>?>
res: MutableList<MutableList<Int>?>
) {
// 子集和等于 target 时,记录解
if (target == 0) {
res.add(ArrayList(state))
res.add(state.toMutableList())
return
}
// 遍历所有选择
@ -998,11 +998,11 @@ comments: true
}
/* 求解子集和 I */
fun subsetSumI(nums: IntArray, target: Int): List<List<Int>?> {
val state: MutableList<Int> = ArrayList() // 状态(子集)
Arrays.sort(nums) // 对 nums 进行排序
fun subsetSumI(nums: IntArray, target: Int): MutableList<MutableList<Int>?> {
val state = mutableListOf<Int>() // 状态(子集)
nums.sort() // 对 nums 进行排序
val start = 0 // 遍历起始点
val res: MutableList<List<Int>?> = ArrayList() // 结果列表(子集列表)
val res = mutableListOf<MutableList<Int>?>() // 结果列表(子集列表)
backtrack(state, target, nums, start, res)
return res
}
@ -1555,11 +1555,11 @@ comments: true
target: Int,
choices: IntArray,
start: Int,
res: MutableList<List<Int>?>
res: MutableList<MutableList<Int>?>
) {
// 子集和等于 target 时,记录解
if (target == 0) {
res.add(ArrayList(state))
res.add(state.toMutableList())
return
}
// 遍历所有选择
@ -1585,11 +1585,11 @@ comments: true
}
/* 求解子集和 II */
fun subsetSumII(nums: IntArray, target: Int): List<List<Int>?> {
val state: MutableList<Int> = ArrayList() // 状态(子集)
Arrays.sort(nums) // 对 nums 进行排序
fun subsetSumII(nums: IntArray, target: Int): MutableList<MutableList<Int>?> {
val state = mutableListOf<Int>() // 状态(子集)
nums.sort() // 对 nums 进行排序
val start = 0 // 遍历起始点
val res: MutableList<List<Int>?> = ArrayList() // 结果列表(子集列表)
val res = mutableListOf<MutableList<Int>?>() // 结果列表(子集列表)
backtrack(state, target, nums, start, res)
return res
}