Skip to content

Commit 16350b6

Browse files
authored
Add kotlin code block for array.md and backtracking_algorithm.md. (krahets#1185)
1 parent 556af16 commit 16350b6

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

docs/chapter_array_and_linkedlist/array.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@
108108
=== "Kotlin"
109109

110110
```kotlin title="array.kt"
111-
111+
/* 初始化数组 */
112+
var arr = IntArray(5) // { 0, 0, 0, 0, 0 }
113+
var nums = intArrayOf(1, 3, 2, 5, 4)
112114
```
113115

114116
=== "Zig"

docs/chapter_backtracking/backtracking_algorithm.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,27 @@
380380
=== "Kotlin"
381381

382382
```kotlin title=""
383-
383+
/* 回溯算法框架 */
384+
fun backtrack(state: State?, choices: List<Choice?>, res: List<State?>?) {
385+
// 判断是否为解
386+
if (isSolution(state)) {
387+
// 记录解
388+
recordSolution(state, res)
389+
// 不再继续搜索
390+
return
391+
}
392+
// 遍历所有选择
393+
for (choice in choices) {
394+
// 剪枝:判断选择是否合法
395+
if (isValid(state, choice)) {
396+
// 尝试:做出选择,更新状态
397+
makeChoice(state, choice)
398+
backtrack(state, choices, res)
399+
// 回退:撤销选择,恢复到之前的状态
400+
undoChoice(state, choice)
401+
}
402+
}
403+
}
384404
```
385405

386406
=== "Zig"

0 commit comments

Comments
 (0)