mirror of
https://github.com/krahets/hello-algo.git
synced 2025-01-23 14:20:29 +08:00
Update the codes of backtracking.
This commit is contained in:
parent
9c070a028f
commit
bc77a81330
@ -21,6 +21,7 @@ void backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &sel
|
||||
// 尝试:做出选择,更新状态
|
||||
selected[i] = true;
|
||||
state.push_back(choice);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, choices, selected, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false;
|
||||
|
@ -23,6 +23,7 @@ void backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &sel
|
||||
duplicated.emplace(choice); // 记录选择过的元素值
|
||||
selected[i] = true;
|
||||
state.push_back(choice);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, choices, selected, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false;
|
||||
|
@ -45,6 +45,7 @@ void backtrack(vector<TreeNode *> &state, vector<TreeNode *> &choices, vector<ve
|
||||
if (isValid(state, choice)) {
|
||||
// 尝试:做出选择,更新状态
|
||||
makeChoice(state, choice);
|
||||
// 进行下一轮选择
|
||||
vector<TreeNode *> nextChoices{choice->left, choice->right};
|
||||
backtrack(state, nextChoices, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
|
@ -25,6 +25,7 @@ public class permutations_i {
|
||||
// 尝试:做出选择,更新状态
|
||||
selected[i] = true;
|
||||
state.Add(choice);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, choices, selected, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false;
|
||||
|
@ -27,6 +27,7 @@ public class permutations_ii {
|
||||
duplicated.Add(choice); // 记录选择过的元素值
|
||||
selected[i] = true;
|
||||
state.Add(choice);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, choices, selected, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false;
|
||||
|
@ -49,8 +49,8 @@ public class preorder_traversal_iii_template {
|
||||
if (isValid(state, choice)) {
|
||||
// 尝试:做出选择,更新状态
|
||||
makeChoice(state, choice);
|
||||
List<TreeNode> nextChoices = new List<TreeNode>() { choice.left, choice.right };
|
||||
backtrack(state, nextChoices, res);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, new List<TreeNode> { choice.left, choice.right }, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
undoChoice(state, choice);
|
||||
}
|
||||
|
@ -24,6 +24,7 @@ public class permutations_i {
|
||||
// 尝试:做出选择,更新状态
|
||||
selected[i] = true;
|
||||
state.add(choice);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, choices, selected, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false;
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
|
@ -25,6 +25,7 @@ def backtrack(
|
||||
# 尝试:做出选择,更新状态
|
||||
selected[i] = True
|
||||
state.append(choice)
|
||||
# 进行下一轮选择
|
||||
backtrack(state, choices, selected, res)
|
||||
# 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = False
|
||||
|
@ -27,6 +27,7 @@ def backtrack(
|
||||
duplicated.add(choice) # 记录选择过的元素值
|
||||
selected[i] = True
|
||||
state.append(choice)
|
||||
# 进行下一轮选择
|
||||
backtrack(state, choices, selected, res)
|
||||
# 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = False
|
||||
|
@ -48,6 +48,7 @@ def backtrack(state: list[TreeNode], choices: list[TreeNode], res: list[list[Tre
|
||||
if is_valid(state, choice):
|
||||
# 尝试:做出选择,更新状态
|
||||
make_choice(state, choice)
|
||||
# 进行下一轮选择
|
||||
backtrack(state, [choice.left, choice.right], res)
|
||||
# 回退:撤销选择,恢复到之前的状态
|
||||
undo_choice(state, choice)
|
||||
|
Loading…
Reference in New Issue
Block a user