mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-24 02:03:10 +08:00
build
This commit is contained in:
@ -217,6 +217,12 @@ comments: true
|
||||
}
|
||||
```
|
||||
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="preorder_traversal_i_compact.rb"
|
||||
[class]{}-[func]{pre_order}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="preorder_traversal_i_compact.zig"
|
||||
@ -513,6 +519,12 @@ comments: true
|
||||
}
|
||||
```
|
||||
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="preorder_traversal_ii_compact.rb"
|
||||
[class]{}-[func]{pre_order}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="preorder_traversal_ii_compact.zig"
|
||||
@ -851,6 +863,12 @@ comments: true
|
||||
}
|
||||
```
|
||||
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="preorder_traversal_iii_compact.rb"
|
||||
[class]{}-[func]{pre_order}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="preorder_traversal_iii_compact.zig"
|
||||
@ -1159,6 +1177,32 @@ comments: true
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title=""
|
||||
/* 回溯算法框架 */
|
||||
fun backtrack(state: State?, choices: List<Choice?>, res: List<State?>?) {
|
||||
// 判断是否为解
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title=""
|
||||
|
||||
```
|
||||
|
||||
@ -1796,6 +1840,22 @@ comments: true
|
||||
}
|
||||
```
|
||||
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="preorder_traversal_iii_template.rb"
|
||||
[class]{}-[func]{is_solution}
|
||||
|
||||
[class]{}-[func]{record_solution}
|
||||
|
||||
[class]{}-[func]{is_valid}
|
||||
|
||||
[class]{}-[func]{make_choice}
|
||||
|
||||
[class]{}-[func]{undo_choice}
|
||||
|
||||
[class]{}-[func]{backtrack}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="preorder_traversal_iii_template.zig"
|
||||
|
Reference in New Issue
Block a user