Update the codes of backtracking.

This commit is contained in:
krahets
2023-04-27 02:17:04 +08:00
parent 9c070a028f
commit bc77a81330
12 changed files with 13 additions and 2 deletions

View File

@ -24,6 +24,7 @@ public class permutations_i {
// 尝试:做出选择,更新状态
selected[i] = true;
state.add(choice);
// 进行下一轮选择
backtrack(state, choices, selected, res);
// 回退:撤销选择,恢复到之前的状态
selected[i] = false;

View File

@ -26,6 +26,7 @@ public class permutations_ii {
duplicated.add(choice); // 记录选择过的元素值
selected[i] = true;
state.add(choice);
// 进行下一轮选择
backtrack(state, choices, selected, res);
// 回退:撤销选择,恢复到之前的状态
selected[i] = false;

View File

@ -49,6 +49,7 @@ public class preorder_traversal_iii_template {
if (isValid(state, choice)) {
// 尝试:做出选择,更新状态
makeChoice(state, choice);
// 进行下一轮选择
backtrack(state, Arrays.asList(choice.left, choice.right), res);
// 回退:撤销选择,恢复到之前的状态
undoChoice(state, choice);