mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-24 10:14:44 +08:00
build
This commit is contained in:
@ -200,6 +200,23 @@ comments: true
|
||||
}
|
||||
```
|
||||
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title="preorder_traversal_i_compact.kt"
|
||||
/* 前序遍历:例题一 */
|
||||
fun preOrder(root: TreeNode?) {
|
||||
if (root == null) {
|
||||
return
|
||||
}
|
||||
if (root.value == 7) {
|
||||
// 记录解
|
||||
res!!.add(root)
|
||||
}
|
||||
preOrder(root.left)
|
||||
preOrder(root.right)
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="preorder_traversal_i_compact.zig"
|
||||
@ -475,6 +492,27 @@ comments: true
|
||||
}
|
||||
```
|
||||
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title="preorder_traversal_ii_compact.kt"
|
||||
/* 前序遍历:例题二 */
|
||||
fun preOrder(root: TreeNode?) {
|
||||
if (root == null) {
|
||||
return
|
||||
}
|
||||
// 尝试
|
||||
path!!.add(root)
|
||||
if (root.value == 7) {
|
||||
// 记录解
|
||||
res!!.add(ArrayList(path!!))
|
||||
}
|
||||
preOrder(root.left)
|
||||
preOrder(root.right)
|
||||
// 回退
|
||||
path!!.removeAt(path!!.size - 1)
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="preorder_traversal_ii_compact.zig"
|
||||
@ -791,6 +829,28 @@ comments: true
|
||||
}
|
||||
```
|
||||
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title="preorder_traversal_iii_compact.kt"
|
||||
/* 前序遍历:例题三 */
|
||||
fun preOrder(root: TreeNode?) {
|
||||
// 剪枝
|
||||
if (root == null || root.value == 3) {
|
||||
return
|
||||
}
|
||||
// 尝试
|
||||
path!!.add(root)
|
||||
if (root.value == 7) {
|
||||
// 记录解
|
||||
res!!.add(ArrayList(path!!))
|
||||
}
|
||||
preOrder(root.left)
|
||||
preOrder(root.right)
|
||||
// 回退
|
||||
path!!.removeAt(path!!.size - 1)
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="preorder_traversal_iii_compact.zig"
|
||||
@ -1096,6 +1156,12 @@ comments: true
|
||||
}
|
||||
```
|
||||
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title=""
|
||||
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title=""
|
||||
@ -1676,6 +1742,60 @@ comments: true
|
||||
}
|
||||
```
|
||||
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title="preorder_traversal_iii_template.kt"
|
||||
/* 判断当前状态是否为解 */
|
||||
fun isSolution(state: List<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 isValid(state: List<TreeNode?>?, choice: TreeNode?): Boolean {
|
||||
return choice != null && choice.value != 3
|
||||
}
|
||||
|
||||
/* 更新状态 */
|
||||
fun makeChoice(state: MutableList<TreeNode?>, choice: TreeNode?) {
|
||||
state.add(choice)
|
||||
}
|
||||
|
||||
/* 恢复状态 */
|
||||
fun undoChoice(state: MutableList<TreeNode?>, choice: TreeNode?) {
|
||||
state.removeLast()
|
||||
}
|
||||
|
||||
/* 回溯算法:例题三 */
|
||||
fun backtrack(
|
||||
state: MutableList<TreeNode?>,
|
||||
choices: List<TreeNode?>,
|
||||
res: MutableList<List<TreeNode?>?>
|
||||
) {
|
||||
// 检查是否为解
|
||||
if (isSolution(state)) {
|
||||
// 记录解
|
||||
recordSolution(state, res)
|
||||
}
|
||||
// 遍历所有选择
|
||||
for (choice in choices) {
|
||||
// 剪枝:检查选择是否合法
|
||||
if (isValid(state, choice)) {
|
||||
// 尝试:做出选择,更新状态
|
||||
makeChoice(state, choice)
|
||||
// 进行下一轮选择
|
||||
backtrack(state, listOf(choice!!.left, choice.right), res)
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
undoChoice(state, choice)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="preorder_traversal_iii_template.zig"
|
||||
|
@ -639,6 +639,73 @@ comments: true
|
||||
}
|
||||
```
|
||||
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title="n_queens.kt"
|
||||
/* 回溯算法:n 皇后 */
|
||||
fun backtrack(
|
||||
row: Int,
|
||||
n: Int,
|
||||
state: List<MutableList<String>>,
|
||||
res: MutableList<List<List<String>>?>,
|
||||
cols: BooleanArray,
|
||||
diags1: BooleanArray,
|
||||
diags2: BooleanArray
|
||||
) {
|
||||
// 当放置完所有行时,记录解
|
||||
if (row == n) {
|
||||
val copyState: MutableList<List<String>> = ArrayList()
|
||||
for (sRow in state) {
|
||||
copyState.add(ArrayList(sRow))
|
||||
}
|
||||
res.add(copyState)
|
||||
return
|
||||
}
|
||||
// 遍历所有列
|
||||
for (col in 0..<n) {
|
||||
// 计算该格子对应的主对角线和次对角线
|
||||
val diag1 = row - col + n - 1
|
||||
val diag2 = row + col
|
||||
// 剪枝:不允许该格子所在列、主对角线、次对角线上存在皇后
|
||||
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
|
||||
// 尝试:将皇后放置在该格子
|
||||
state[row][col] = "Q"
|
||||
diags2[diag2] = true
|
||||
diags1[diag1] = diags2[diag2]
|
||||
cols[col] = diags1[diag1]
|
||||
// 放置下一行
|
||||
backtrack(row + 1, n, state, res, cols, diags1, diags2)
|
||||
// 回退:将该格子恢复为空位
|
||||
state[row][col] = "#"
|
||||
diags2[diag2] = false
|
||||
diags1[diag1] = diags2[diag2]
|
||||
cols[col] = diags1[diag1]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 求解 n 皇后 */
|
||||
fun nQueens(n: Int): List<List<List<String>>?> {
|
||||
// 初始化 n*n 大小的棋盘,其中 'Q' 代表皇后,'#' 代表空位
|
||||
val state: MutableList<MutableList<String>> = ArrayList()
|
||||
for (i in 0..<n) {
|
||||
val row: MutableList<String> = ArrayList()
|
||||
for (j in 0..<n) {
|
||||
row.add("#")
|
||||
}
|
||||
state.add(row)
|
||||
}
|
||||
val cols = BooleanArray(n) // 记录列是否有皇后
|
||||
val diags1 = BooleanArray(2 * n - 1) // 记录主对角线上是否有皇后
|
||||
val diags2 = BooleanArray(2 * n - 1) // 记录次对角线上是否有皇后
|
||||
val res: MutableList<List<List<String>>?> = ArrayList()
|
||||
|
||||
backtrack(0, n, state, res, cols, diags1, diags2)
|
||||
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="n_queens.zig"
|
||||
|
@ -463,6 +463,46 @@ comments: true
|
||||
}
|
||||
```
|
||||
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title="permutations_i.kt"
|
||||
/* 回溯算法:全排列 I */
|
||||
fun backtrack(
|
||||
state: MutableList<Int>,
|
||||
choices: IntArray,
|
||||
selected: BooleanArray,
|
||||
res: MutableList<List<Int>?>
|
||||
) {
|
||||
// 当状态长度等于元素数量时,记录解
|
||||
if (state.size == choices.size) {
|
||||
res.add(ArrayList(state))
|
||||
return
|
||||
}
|
||||
// 遍历所有选择
|
||||
for (i in choices.indices) {
|
||||
val choice = choices[i]
|
||||
// 剪枝:不允许重复选择元素
|
||||
if (!selected[i]) {
|
||||
// 尝试:做出选择,更新状态
|
||||
selected[i] = true
|
||||
state.add(choice)
|
||||
// 进行下一轮选择
|
||||
backtrack(state, choices, selected, res)
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false
|
||||
state.removeAt(state.size - 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 全排列 I */
|
||||
fun permutationsI(nums: IntArray): List<List<Int>?> {
|
||||
val res: MutableList<List<Int>?> = ArrayList()
|
||||
backtrack(ArrayList(), nums, BooleanArray(nums.size), res)
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="permutations_i.zig"
|
||||
@ -939,6 +979,48 @@ comments: true
|
||||
}
|
||||
```
|
||||
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title="permutations_ii.kt"
|
||||
/* 回溯算法:全排列 II */
|
||||
fun backtrack(
|
||||
state: MutableList<Int>,
|
||||
choices: IntArray,
|
||||
selected: BooleanArray,
|
||||
res: MutableList<MutableList<Int>?>
|
||||
) {
|
||||
// 当状态长度等于元素数量时,记录解
|
||||
if (state.size == choices.size) {
|
||||
res.add(ArrayList(state))
|
||||
return
|
||||
}
|
||||
// 遍历所有选择
|
||||
val duplicated: MutableSet<Int> = HashSet()
|
||||
for (i in choices.indices) {
|
||||
val choice = choices[i]
|
||||
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||
if (!selected[i] && !duplicated.contains(choice)) {
|
||||
// 尝试:做出选择,更新状态
|
||||
duplicated.add(choice) // 记录选择过的元素值
|
||||
selected[i] = true
|
||||
state.add(choice)
|
||||
// 进行下一轮选择
|
||||
backtrack(state, choices, selected, res)
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false
|
||||
state.removeAt(state.size - 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 全排列 II */
|
||||
fun permutationsII(nums: IntArray): MutableList<MutableList<Int>?> {
|
||||
val res: MutableList<MutableList<Int>?> = ArrayList()
|
||||
backtrack(ArrayList(), nums, BooleanArray(nums.size), res)
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="permutations_ii.zig"
|
||||
|
@ -426,6 +426,47 @@ comments: true
|
||||
}
|
||||
```
|
||||
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title="subset_sum_i_naive.kt"
|
||||
/* 回溯算法:子集和 I */
|
||||
fun backtrack(
|
||||
state: MutableList<Int>,
|
||||
target: Int,
|
||||
total: Int,
|
||||
choices: IntArray,
|
||||
res: MutableList<List<Int>?>
|
||||
) {
|
||||
// 子集和等于 target 时,记录解
|
||||
if (total == target) {
|
||||
res.add(ArrayList(state))
|
||||
return
|
||||
}
|
||||
// 遍历所有选择
|
||||
for (i in choices.indices) {
|
||||
// 剪枝:若子集和超过 target ,则跳过该选择
|
||||
if (total + choices[i] > target) {
|
||||
continue
|
||||
}
|
||||
// 尝试:做出选择,更新元素和 total
|
||||
state.add(choices[i])
|
||||
// 进行下一轮选择
|
||||
backtrack(state, target, total + choices[i], choices, res)
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
state.removeAt(state.size - 1)
|
||||
}
|
||||
}
|
||||
|
||||
/* 求解子集和 I(包含重复子集) */
|
||||
fun subsetSumINaive(nums: IntArray, target: Int): List<List<Int>?> {
|
||||
val state: MutableList<Int> = ArrayList() // 状态(子集)
|
||||
val total = 0 // 子集和
|
||||
val res: MutableList<List<Int>?> = ArrayList() // 结果列表(子集列表)
|
||||
backtrack(state, target, total, nums, res)
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="subset_sum_i_naive.zig"
|
||||
@ -915,6 +956,50 @@ comments: true
|
||||
}
|
||||
```
|
||||
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title="subset_sum_i.kt"
|
||||
/* 回溯算法:子集和 I */
|
||||
fun backtrack(
|
||||
state: MutableList<Int>,
|
||||
target: Int,
|
||||
choices: IntArray,
|
||||
start: Int,
|
||||
res: MutableList<List<Int>?>
|
||||
) {
|
||||
// 子集和等于 target 时,记录解
|
||||
if (target == 0) {
|
||||
res.add(ArrayList(state))
|
||||
return
|
||||
}
|
||||
// 遍历所有选择
|
||||
// 剪枝二:从 start 开始遍历,避免生成重复子集
|
||||
for (i in start..<choices.size) {
|
||||
// 剪枝一:若子集和超过 target ,则直接结束循环
|
||||
// 这是因为数组已排序,后边元素更大,子集和一定超过 target
|
||||
if (target - choices[i] < 0) {
|
||||
break
|
||||
}
|
||||
// 尝试:做出选择,更新 target, start
|
||||
state.add(choices[i])
|
||||
// 进行下一轮选择
|
||||
backtrack(state, target - choices[i], choices, i, res)
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
state.removeAt(state.size - 1)
|
||||
}
|
||||
}
|
||||
|
||||
/* 求解子集和 I */
|
||||
fun subsetSumI(nums: IntArray, target: Int): List<List<Int>?> {
|
||||
val state: MutableList<Int> = ArrayList() // 状态(子集)
|
||||
Arrays.sort(nums) // 对 nums 进行排序
|
||||
val start = 0 // 遍历起始点
|
||||
val res: MutableList<List<Int>?> = ArrayList() // 结果列表(子集列表)
|
||||
backtrack(state, target, nums, start, res)
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="subset_sum_i.zig"
|
||||
@ -1445,6 +1530,55 @@ comments: true
|
||||
}
|
||||
```
|
||||
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title="subset_sum_ii.kt"
|
||||
/* 回溯算法:子集和 II */
|
||||
fun backtrack(
|
||||
state: MutableList<Int>,
|
||||
target: Int,
|
||||
choices: IntArray,
|
||||
start: Int,
|
||||
res: MutableList<List<Int>?>
|
||||
) {
|
||||
// 子集和等于 target 时,记录解
|
||||
if (target == 0) {
|
||||
res.add(ArrayList(state))
|
||||
return
|
||||
}
|
||||
// 遍历所有选择
|
||||
// 剪枝二:从 start 开始遍历,避免生成重复子集
|
||||
// 剪枝三:从 start 开始遍历,避免重复选择同一元素
|
||||
for (i in start..<choices.size) {
|
||||
// 剪枝一:若子集和超过 target ,则直接结束循环
|
||||
// 这是因为数组已排序,后边元素更大,子集和一定超过 target
|
||||
if (target - choices[i] < 0) {
|
||||
break
|
||||
}
|
||||
// 剪枝四:如果该元素与左边元素相等,说明该搜索分支重复,直接跳过
|
||||
if (i > start && choices[i] == choices[i - 1]) {
|
||||
continue
|
||||
}
|
||||
// 尝试:做出选择,更新 target, start
|
||||
state.add(choices[i])
|
||||
// 进行下一轮选择
|
||||
backtrack(state, target - choices[i], choices, i + 1, res)
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
state.removeAt(state.size - 1)
|
||||
}
|
||||
}
|
||||
|
||||
/* 求解子集和 II */
|
||||
fun subsetSumII(nums: IntArray, target: Int): List<List<Int>?> {
|
||||
val state: MutableList<Int> = ArrayList() // 状态(子集)
|
||||
Arrays.sort(nums) // 对 nums 进行排序
|
||||
val start = 0 // 遍历起始点
|
||||
val res: MutableList<List<Int>?> = ArrayList() // 结果列表(子集列表)
|
||||
backtrack(state, target, nums, start, res)
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="subset_sum_ii.zig"
|
||||
|
Reference in New Issue
Block a user