This commit is contained in:
krahets
2023-10-08 01:43:28 +08:00
parent 3d2d669b43
commit baac2d11a7
52 changed files with 999 additions and 625 deletions
@@ -66,7 +66,7 @@ comments: true
```csharp title="preorder_traversal_i_compact.cs"
/* 前序遍历:例题一 */
void preOrder(TreeNode root) {
void PreOrder(TreeNode root) {
if (root == null) {
return;
}
@@ -74,8 +74,8 @@ comments: true
// 记录解
res.Add(root);
}
preOrder(root.left);
preOrder(root.right);
PreOrder(root.left);
PreOrder(root.right);
}
```
@@ -277,7 +277,7 @@ comments: true
```csharp title="preorder_traversal_ii_compact.cs"
/* 前序遍历:例题二 */
void preOrder(TreeNode root) {
void PreOrder(TreeNode root) {
if (root == null) {
return;
}
@@ -287,8 +287,8 @@ comments: true
// 记录解
res.Add(new List<TreeNode>(path));
}
preOrder(root.left);
preOrder(root.right);
PreOrder(root.left);
PreOrder(root.right);
// 回退
path.RemoveAt(path.Count - 1);
}
@@ -580,7 +580,7 @@ comments: true
```csharp title="preorder_traversal_iii_compact.cs"
/* 前序遍历:例题三 */
void preOrder(TreeNode root) {
void PreOrder(TreeNode root) {
// 剪枝
if (root == null || root.val == 3) {
return;
@@ -591,8 +591,8 @@ comments: true
// 记录解
res.Add(new List<TreeNode>(path));
}
preOrder(root.left);
preOrder(root.right);
PreOrder(root.left);
PreOrder(root.right);
// 回退
path.RemoveAt(path.Count - 1);
}
@@ -865,23 +865,23 @@ comments: true
```csharp title=""
/* 回溯算法框架 */
void backtrack(State state, List<Choice> choices, List<State> res) {
void Backtrack(State state, List<Choice> choices, List<State> res) {
// 判断是否为解
if (isSolution(state)) {
if (IsSolution(state)) {
// 记录解
recordSolution(state, res);
RecordSolution(state, res);
// 停止继续搜索
return;
}
// 遍历所有选择
foreach (Choice choice in choices) {
// 剪枝:判断选择是否合法
if (isValid(state, choice)) {
if (IsValid(state, choice)) {
// 尝试:做出选择,更新状态
makeChoice(state, choice);
backtrack(state, choices, res);
MakeChoice(state, choice);
Backtrack(state, choices, res);
// 回退:撤销选择,恢复到之前的状态
undoChoice(state, choice);
UndoChoice(state, choice);
}
}
}
@@ -1225,47 +1225,47 @@ comments: true
```csharp title="preorder_traversal_iii_template.cs"
/* 判断当前状态是否为解 */
bool isSolution(List<TreeNode> state) {
bool IsSolution(List<TreeNode> state) {
return state.Count != 0 && state[^1].val == 7;
}
/* 记录解 */
void recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {
void RecordSolution(List<TreeNode> state, List<List<TreeNode>> res) {
res.Add(new List<TreeNode>(state));
}
/* 判断在当前状态下,该选择是否合法 */
bool isValid(List<TreeNode> state, TreeNode choice) {
bool IsValid(List<TreeNode> state, TreeNode choice) {
return choice != null && choice.val != 3;
}
/* 更新状态 */
void makeChoice(List<TreeNode> state, TreeNode choice) {
void MakeChoice(List<TreeNode> state, TreeNode choice) {
state.Add(choice);
}
/* 恢复状态 */
void undoChoice(List<TreeNode> state, TreeNode choice) {
void UndoChoice(List<TreeNode> state, TreeNode choice) {
state.RemoveAt(state.Count - 1);
}
/* 回溯算法:例题三 */
void backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {
void Backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {
// 检查是否为解
if (isSolution(state)) {
if (IsSolution(state)) {
// 记录解
recordSolution(state, res);
RecordSolution(state, res);
}
// 遍历所有选择
foreach (TreeNode choice in choices) {
// 剪枝:检查选择是否合法
if (isValid(state, choice)) {
if (IsValid(state, choice)) {
// 尝试:做出选择,更新状态
makeChoice(state, choice);
MakeChoice(state, choice);
// 进行下一轮选择
backtrack(state, new List<TreeNode> { choice.left, choice.right }, res);
Backtrack(state, new List<TreeNode> { choice.left, choice.right }, res);
// 回退:撤销选择,恢复到之前的状态
undoChoice(state, choice);
UndoChoice(state, choice);
}
}
}
+8 -8
View File
@@ -203,11 +203,11 @@ comments: true
```csharp title="n_queens.cs"
/* 回溯算法:N 皇后 */
void backtrack(int row, int n, List<List<string>> state, List<List<List<string>>> res,
void Backtrack(int row, int n, List<List<string>> state, List<List<List<string>>> res,
bool[] cols, bool[] diags1, bool[] diags2) {
// 当放置完所有行时,记录解
if (row == n) {
List<List<string>> copyState = new List<List<string>>();
List<List<string>> copyState = new();
foreach (List<string> sRow in state) {
copyState.Add(new List<string>(sRow));
}
@@ -225,7 +225,7 @@ comments: true
state[row][col] = "Q";
cols[col] = diags1[diag1] = diags2[diag2] = true;
// 放置下一行
backtrack(row + 1, n, state, res, cols, diags1, diags2);
Backtrack(row + 1, n, state, res, cols, diags1, diags2);
// 回退:将该格子恢复为空位
state[row][col] = "#";
cols[col] = diags1[diag1] = diags2[diag2] = false;
@@ -234,11 +234,11 @@ comments: true
}
/* 求解 N 皇后 */
List<List<List<string>>> nQueens(int n) {
List<List<List<string>>> NQueens(int n) {
// 初始化 n*n 大小的棋盘,其中 'Q' 代表皇后,'#' 代表空位
List<List<string>> state = new List<List<string>>();
List<List<string>> state = new();
for (int i = 0; i < n; i++) {
List<string> row = new List<string>();
List<string> row = new();
for (int j = 0; j < n; j++) {
row.Add("#");
}
@@ -247,9 +247,9 @@ comments: true
bool[] cols = new bool[n]; // 记录列是否有皇后
bool[] diags1 = new bool[2 * n - 1]; // 记录主对角线是否有皇后
bool[] diags2 = new bool[2 * n - 1]; // 记录副对角线是否有皇后
List<List<List<string>>> res = new List<List<List<string>>>();
List<List<List<string>>> res = new();
backtrack(0, n, state, res, cols, diags1, diags2);
Backtrack(0, n, state, res, cols, diags1, diags2);
return res;
}
+10 -10
View File
@@ -162,7 +162,7 @@ comments: true
```csharp title="permutations_i.cs"
/* 回溯算法:全排列 I */
void backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {
void Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {
// 当状态长度等于元素数量时,记录解
if (state.Count == choices.Length) {
res.Add(new List<int>(state));
@@ -177,7 +177,7 @@ comments: true
selected[i] = true;
state.Add(choice);
// 进行下一轮选择
backtrack(state, choices, selected, res);
Backtrack(state, choices, selected, res);
// 回退:撤销选择,恢复到之前的状态
selected[i] = false;
state.RemoveAt(state.Count - 1);
@@ -186,9 +186,9 @@ comments: true
}
/* 全排列 I */
List<List<int>> permutationsI(int[] nums) {
List<List<int>> res = new List<List<int>>();
backtrack(new List<int>(), nums, new bool[nums.Length], res);
List<List<int>> PermutationsI(int[] nums) {
List<List<int>> res = new();
Backtrack(new List<int>(), nums, new bool[nums.Length], res);
return res;
}
```
@@ -618,7 +618,7 @@ comments: true
```csharp title="permutations_ii.cs"
/* 回溯算法:全排列 II */
void backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {
void Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {
// 当状态长度等于元素数量时,记录解
if (state.Count == choices.Length) {
res.Add(new List<int>(state));
@@ -635,7 +635,7 @@ comments: true
selected[i] = true;
state.Add(choice);
// 进行下一轮选择
backtrack(state, choices, selected, res);
Backtrack(state, choices, selected, res);
// 回退:撤销选择,恢复到之前的状态
selected[i] = false;
state.RemoveAt(state.Count - 1);
@@ -644,9 +644,9 @@ comments: true
}
/* 全排列 II */
List<List<int>> permutationsII(int[] nums) {
List<List<int>> res = new List<List<int>>();
backtrack(new List<int>(), nums, new bool[nums.Length], res);
List<List<int>> PermutationsII(int[] nums) {
List<List<int>> res = new();
Backtrack(new List<int>(), nums, new bool[nums.Length], res);
return res;
}
```
+18 -18
View File
@@ -131,7 +131,7 @@ comments: true
```csharp title="subset_sum_i_naive.cs"
/* 回溯算法:子集和 I */
void backtrack(List<int> state, int target, int total, int[] choices, List<List<int>> res) {
void Backtrack(List<int> state, int target, int total, int[] choices, List<List<int>> res) {
// 子集和等于 target 时,记录解
if (total == target) {
res.Add(new List<int>(state));
@@ -146,18 +146,18 @@ comments: true
// 尝试:做出选择,更新元素和 total
state.Add(choices[i]);
// 进行下一轮选择
backtrack(state, target, total + choices[i], choices, res);
Backtrack(state, target, total + choices[i], choices, res);
// 回退:撤销选择,恢复到之前的状态
state.RemoveAt(state.Count - 1);
}
}
/* 求解子集和 I(包含重复子集) */
List<List<int>> subsetSumINaive(int[] nums, int target) {
List<int> state = new List<int>(); // 状态(子集)
List<List<int>> SubsetSumINaive(int[] nums, int target) {
List<int> state = new(); // 状态(子集)
int total = 0; // 子集和
List<List<int>> res = new List<List<int>>(); // 结果列表(子集列表)
backtrack(state, target, total, nums, res);
List<List<int>> res = new(); // 结果列表(子集列表)
Backtrack(state, target, total, nums, res);
return res;
}
```
@@ -588,7 +588,7 @@ comments: true
```csharp title="subset_sum_i.cs"
/* 回溯算法:子集和 I */
void backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {
void Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {
// 子集和等于 target 时,记录解
if (target == 0) {
res.Add(new List<int>(state));
@@ -605,19 +605,19 @@ comments: true
// 尝试:做出选择,更新 target, start
state.Add(choices[i]);
// 进行下一轮选择
backtrack(state, target - choices[i], choices, i, res);
Backtrack(state, target - choices[i], choices, i, res);
// 回退:撤销选择,恢复到之前的状态
state.RemoveAt(state.Count - 1);
}
}
/* 求解子集和 I */
List<List<int>> subsetSumI(int[] nums, int target) {
List<int> state = new List<int>(); // 状态(子集)
List<List<int>> SubsetSumI(int[] nums, int target) {
List<int> state = new(); // 状态(子集)
Array.Sort(nums); // 对 nums 进行排序
int start = 0; // 遍历起始点
List<List<int>> res = new List<List<int>>(); // 结果列表(子集列表)
backtrack(state, target, nums, start, res);
List<List<int>> res = new(); // 结果列表(子集列表)
Backtrack(state, target, nums, start, res);
return res;
}
```
@@ -1069,7 +1069,7 @@ comments: true
```csharp title="subset_sum_ii.cs"
/* 回溯算法:子集和 II */
void backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {
void Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {
// 子集和等于 target 时,记录解
if (target == 0) {
res.Add(new List<int>(state));
@@ -1091,19 +1091,19 @@ comments: true
// 尝试:做出选择,更新 target, start
state.Add(choices[i]);
// 进行下一轮选择
backtrack(state, target - choices[i], choices, i + 1, res);
Backtrack(state, target - choices[i], choices, i + 1, res);
// 回退:撤销选择,恢复到之前的状态
state.RemoveAt(state.Count - 1);
}
}
/* 求解子集和 II */
List<List<int>> subsetSumII(int[] nums, int target) {
List<int> state = new List<int>(); // 状态(子集)
List<List<int>> SubsetSumII(int[] nums, int target) {
List<int> state = new(); // 状态(子集)
Array.Sort(nums); // 对 nums 进行排序
int start = 0; // 遍历起始点
List<List<int>> res = new List<List<int>>(); // 结果列表(子集列表)
backtrack(state, target, nums, start, res);
List<List<int>> res = new(); // 结果列表(子集列表)
Backtrack(state, target, nums, start, res);
return res;
}
```