From 16350b65e4634a19b3cd5f9ab9c44281bf56bbd4 Mon Sep 17 00:00:00 2001 From: curtishd <131777542+curtishd@users.noreply.github.com> Date: Wed, 27 Mar 2024 21:47:34 +0800 Subject: [PATCH] Add kotlin code block for array.md and backtracking_algorithm.md. (#1185) --- docs/chapter_array_and_linkedlist/array.md | 4 +++- .../backtracking_algorithm.md | 22 ++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/docs/chapter_array_and_linkedlist/array.md b/docs/chapter_array_and_linkedlist/array.md index 0625316ff..bf3ee8250 100755 --- a/docs/chapter_array_and_linkedlist/array.md +++ b/docs/chapter_array_and_linkedlist/array.md @@ -108,7 +108,9 @@ === "Kotlin" ```kotlin title="array.kt" - + /* 初始化数组 */ + var arr = IntArray(5) // { 0, 0, 0, 0, 0 } + var nums = intArrayOf(1, 3, 2, 5, 4) ``` === "Zig" diff --git a/docs/chapter_backtracking/backtracking_algorithm.md b/docs/chapter_backtracking/backtracking_algorithm.md index f3a3da5a1..ca4b19885 100644 --- a/docs/chapter_backtracking/backtracking_algorithm.md +++ b/docs/chapter_backtracking/backtracking_algorithm.md @@ -380,7 +380,27 @@ === "Kotlin" ```kotlin title="" - + /* 回溯算法框架 */ + fun backtrack(state: State?, choices: List, res: List?) { + // 判断是否为解 + if (isSolution(state)) { + // 记录解 + recordSolution(state, res) + // 不再继续搜索 + return + } + // 遍历所有选择 + for (choice in choices) { + // 剪枝:判断选择是否合法 + if (isValid(state, choice)) { + // 尝试:做出选择,更新状态 + makeChoice(state, choice) + backtrack(state, choices, res) + // 回退:撤销选择,恢复到之前的状态 + undoChoice(state, choice) + } + } + } ``` === "Zig"