mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-05 11:41:22 +08:00
fix(csharp): Modify method name to PascalCase, simplify new expression (#840)
* Modify method name to PascalCase(array and linked list) * Modify method name to PascalCase(backtracking) * Modify method name to PascalCase(computational complexity) * Modify method name to PascalCase(divide and conquer) * Modify method name to PascalCase(dynamic programming) * Modify method name to PascalCase(graph) * Modify method name to PascalCase(greedy) * Modify method name to PascalCase(hashing) * Modify method name to PascalCase(heap) * Modify method name to PascalCase(searching) * Modify method name to PascalCase(sorting) * Modify method name to PascalCase(stack and queue) * Modify method name to PascalCase(tree) * local check
This commit is contained in:
@@ -8,11 +8,11 @@ namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class n_queens {
|
||||
/* 回溯算法:N 皇后 */
|
||||
static void backtrack(int row, int n, List<List<string>> state, List<List<List<string>>> res,
|
||||
static 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));
|
||||
}
|
||||
@@ -30,7 +30,7 @@ public class n_queens {
|
||||
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;
|
||||
@@ -39,11 +39,11 @@ public class n_queens {
|
||||
}
|
||||
|
||||
/* 求解 N 皇后 */
|
||||
static List<List<List<string>>> nQueens(int n) {
|
||||
static 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("#");
|
||||
}
|
||||
@@ -52,9 +52,9 @@ public class n_queens {
|
||||
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;
|
||||
}
|
||||
@@ -62,7 +62,7 @@ public class n_queens {
|
||||
[Test]
|
||||
public void Test() {
|
||||
int n = 4;
|
||||
List<List<List<string>>> res = nQueens(n);
|
||||
List<List<List<string>>> res = NQueens(n);
|
||||
|
||||
Console.WriteLine("输入棋盘长宽为 " + n);
|
||||
Console.WriteLine("皇后放置方案共有 " + res.Count + " 种");
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class permutations_i {
|
||||
/* 回溯算法:全排列 I */
|
||||
static void backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {
|
||||
static void Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {
|
||||
// 当状态长度等于元素数量时,记录解
|
||||
if (state.Count == choices.Length) {
|
||||
res.Add(new List<int>(state));
|
||||
@@ -23,7 +23,7 @@ public class permutations_i {
|
||||
selected[i] = true;
|
||||
state.Add(choice);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, choices, selected, res);
|
||||
Backtrack(state, choices, selected, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false;
|
||||
state.RemoveAt(state.Count - 1);
|
||||
@@ -32,9 +32,9 @@ public class permutations_i {
|
||||
}
|
||||
|
||||
/* 全排列 I */
|
||||
static List<List<int>> permutationsI(int[] nums) {
|
||||
List<List<int>> res = new List<List<int>>();
|
||||
backtrack(new List<int>(), nums, new bool[nums.Length], res);
|
||||
static List<List<int>> PermutationsI(int[] nums) {
|
||||
List<List<int>> res = new();
|
||||
Backtrack(new List<int>(), nums, new bool[nums.Length], res);
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ public class permutations_i {
|
||||
public void Test() {
|
||||
int[] nums = { 1, 2, 3 };
|
||||
|
||||
List<List<int>> res = permutationsI(nums);
|
||||
List<List<int>> res = PermutationsI(nums);
|
||||
|
||||
Console.WriteLine("输入数组 nums = " + string.Join(", ", nums));
|
||||
Console.WriteLine("所有排列 res = ");
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class permutations_ii {
|
||||
/* 回溯算法:全排列 II */
|
||||
static void backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {
|
||||
static void Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {
|
||||
// 当状态长度等于元素数量时,记录解
|
||||
if (state.Count == choices.Length) {
|
||||
res.Add(new List<int>(state));
|
||||
@@ -25,7 +25,7 @@ public class permutations_ii {
|
||||
selected[i] = true;
|
||||
state.Add(choice);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, choices, selected, res);
|
||||
Backtrack(state, choices, selected, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
selected[i] = false;
|
||||
state.RemoveAt(state.Count - 1);
|
||||
@@ -34,9 +34,9 @@ public class permutations_ii {
|
||||
}
|
||||
|
||||
/* 全排列 II */
|
||||
static List<List<int>> permutationsII(int[] nums) {
|
||||
List<List<int>> res = new List<List<int>>();
|
||||
backtrack(new List<int>(), nums, new bool[nums.Length], res);
|
||||
static List<List<int>> PermutationsII(int[] nums) {
|
||||
List<List<int>> res = new();
|
||||
Backtrack(new List<int>(), nums, new bool[nums.Length], res);
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ public class permutations_ii {
|
||||
public void Test() {
|
||||
int[] nums = { 1, 2, 2 };
|
||||
|
||||
List<List<int>> res = permutationsII(nums);
|
||||
List<List<int>> res = PermutationsII(nums);
|
||||
|
||||
Console.WriteLine("输入数组 nums = " + string.Join(", ", nums));
|
||||
Console.WriteLine("所有排列 res = ");
|
||||
|
||||
@@ -10,7 +10,7 @@ public class preorder_traversal_i_compact {
|
||||
static List<TreeNode> res;
|
||||
|
||||
/* 前序遍历:例题一 */
|
||||
static void preOrder(TreeNode root) {
|
||||
static void PreOrder(TreeNode root) {
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
@@ -18,8 +18,8 @@ public class preorder_traversal_i_compact {
|
||||
// 记录解
|
||||
res.Add(root);
|
||||
}
|
||||
preOrder(root.left);
|
||||
preOrder(root.right);
|
||||
PreOrder(root.left);
|
||||
PreOrder(root.right);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -30,7 +30,7 @@ public class preorder_traversal_i_compact {
|
||||
|
||||
// 前序遍历
|
||||
res = new List<TreeNode>();
|
||||
preOrder(root);
|
||||
PreOrder(root);
|
||||
|
||||
Console.WriteLine("\n输出所有值为 7 的节点");
|
||||
PrintUtil.PrintList(res.Select(p => p.val).ToList());
|
||||
|
||||
@@ -11,7 +11,7 @@ public class preorder_traversal_ii_compact {
|
||||
static List<List<TreeNode>> res;
|
||||
|
||||
/* 前序遍历:例题二 */
|
||||
static void preOrder(TreeNode root) {
|
||||
static void PreOrder(TreeNode root) {
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
@@ -21,8 +21,8 @@ public class preorder_traversal_ii_compact {
|
||||
// 记录解
|
||||
res.Add(new List<TreeNode>(path));
|
||||
}
|
||||
preOrder(root.left);
|
||||
preOrder(root.right);
|
||||
PreOrder(root.left);
|
||||
PreOrder(root.right);
|
||||
// 回退
|
||||
path.RemoveAt(path.Count - 1);
|
||||
}
|
||||
@@ -36,7 +36,7 @@ public class preorder_traversal_ii_compact {
|
||||
// 前序遍历
|
||||
path = new List<TreeNode>();
|
||||
res = new List<List<TreeNode>>();
|
||||
preOrder(root);
|
||||
PreOrder(root);
|
||||
|
||||
Console.WriteLine("\n输出所有根节点到节点 7 的路径");
|
||||
foreach (List<TreeNode> path in res) {
|
||||
|
||||
@@ -11,7 +11,7 @@ public class preorder_traversal_iii_compact {
|
||||
static List<List<TreeNode>> res;
|
||||
|
||||
/* 前序遍历:例题三 */
|
||||
static void preOrder(TreeNode root) {
|
||||
static void PreOrder(TreeNode root) {
|
||||
// 剪枝
|
||||
if (root == null || root.val == 3) {
|
||||
return;
|
||||
@@ -22,8 +22,8 @@ public class preorder_traversal_iii_compact {
|
||||
// 记录解
|
||||
res.Add(new List<TreeNode>(path));
|
||||
}
|
||||
preOrder(root.left);
|
||||
preOrder(root.right);
|
||||
PreOrder(root.left);
|
||||
PreOrder(root.right);
|
||||
// 回退
|
||||
path.RemoveAt(path.Count - 1);
|
||||
}
|
||||
@@ -37,7 +37,7 @@ public class preorder_traversal_iii_compact {
|
||||
// 前序遍历
|
||||
path = new List<TreeNode>();
|
||||
res = new List<List<TreeNode>>();
|
||||
preOrder(root);
|
||||
PreOrder(root);
|
||||
|
||||
Console.WriteLine("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点");
|
||||
foreach (List<TreeNode> path in res) {
|
||||
|
||||
@@ -8,47 +8,47 @@ namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class preorder_traversal_iii_template {
|
||||
/* 判断当前状态是否为解 */
|
||||
static bool isSolution(List<TreeNode> state) {
|
||||
static bool IsSolution(List<TreeNode> state) {
|
||||
return state.Count != 0 && state[^1].val == 7;
|
||||
}
|
||||
|
||||
/* 记录解 */
|
||||
static void recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {
|
||||
static void RecordSolution(List<TreeNode> state, List<List<TreeNode>> res) {
|
||||
res.Add(new List<TreeNode>(state));
|
||||
}
|
||||
|
||||
/* 判断在当前状态下,该选择是否合法 */
|
||||
static bool isValid(List<TreeNode> state, TreeNode choice) {
|
||||
static bool IsValid(List<TreeNode> state, TreeNode choice) {
|
||||
return choice != null && choice.val != 3;
|
||||
}
|
||||
|
||||
/* 更新状态 */
|
||||
static void makeChoice(List<TreeNode> state, TreeNode choice) {
|
||||
static void MakeChoice(List<TreeNode> state, TreeNode choice) {
|
||||
state.Add(choice);
|
||||
}
|
||||
|
||||
/* 恢复状态 */
|
||||
static void undoChoice(List<TreeNode> state, TreeNode choice) {
|
||||
static void UndoChoice(List<TreeNode> state, TreeNode choice) {
|
||||
state.RemoveAt(state.Count - 1);
|
||||
}
|
||||
|
||||
/* 回溯算法:例题三 */
|
||||
static void backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {
|
||||
static 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -60,9 +60,9 @@ public class preorder_traversal_iii_template {
|
||||
PrintUtil.PrintTree(root);
|
||||
|
||||
// 回溯算法
|
||||
List<List<TreeNode>> res = new List<List<TreeNode>>();
|
||||
List<TreeNode> choices = new List<TreeNode>() { root };
|
||||
backtrack(new List<TreeNode>(), choices, res);
|
||||
List<List<TreeNode>> res = new();
|
||||
List<TreeNode> choices = new() { root };
|
||||
Backtrack(new List<TreeNode>(), choices, res);
|
||||
|
||||
Console.WriteLine("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点");
|
||||
foreach (List<TreeNode> path in res) {
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class subset_sum_i {
|
||||
/* 回溯算法:子集和 I */
|
||||
public static void backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {
|
||||
public static 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));
|
||||
@@ -25,19 +25,19 @@ public class subset_sum_i {
|
||||
// 尝试:做出选择,更新 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 */
|
||||
public static List<List<int>> subsetSumI(int[] nums, int target) {
|
||||
List<int> state = new List<int>(); // 状态(子集)
|
||||
public static 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;
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ public class subset_sum_i {
|
||||
public void Test() {
|
||||
int[] nums = { 3, 4, 5 };
|
||||
int target = 9;
|
||||
List<List<int>> res = subsetSumI(nums, target);
|
||||
List<List<int>> res = SubsetSumI(nums, target);
|
||||
Console.WriteLine("输入数组 nums = " + string.Join(", ", nums) + ", target = " + target);
|
||||
Console.WriteLine("所有和等于 " + target + " 的子集 res = ");
|
||||
foreach (var subset in res) {
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class subset_sum_i_naive {
|
||||
/* 回溯算法:子集和 I */
|
||||
public static void backtrack(List<int> state, int target, int total, int[] choices, List<List<int>> res) {
|
||||
public static 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));
|
||||
@@ -23,18 +23,18 @@ public class subset_sum_i_naive {
|
||||
// 尝试:做出选择,更新元素和 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(包含重复子集) */
|
||||
public static List<List<int>> subsetSumINaive(int[] nums, int target) {
|
||||
List<int> state = new List<int>(); // 状态(子集)
|
||||
public static 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;
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ public class subset_sum_i_naive {
|
||||
public void Test() {
|
||||
int[] nums = { 3, 4, 5 };
|
||||
int target = 9;
|
||||
List<List<int>> res = subsetSumINaive(nums, target);
|
||||
List<List<int>> res = SubsetSumINaive(nums, target);
|
||||
Console.WriteLine("输入数组 nums = " + string.Join(", ", nums) + ", target = " + target);
|
||||
Console.WriteLine("所有和等于 " + target + " 的子集 res = ");
|
||||
foreach (var subset in res) {
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class subset_sum_ii {
|
||||
/* 回溯算法:子集和 II */
|
||||
public static void backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {
|
||||
public static 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));
|
||||
@@ -30,19 +30,19 @@ public class subset_sum_ii {
|
||||
// 尝试:做出选择,更新 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 */
|
||||
public static List<List<int>> subsetSumII(int[] nums, int target) {
|
||||
List<int> state = new List<int>(); // 状态(子集)
|
||||
public static 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;
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ public class subset_sum_ii {
|
||||
public void Test() {
|
||||
int[] nums = { 4, 4, 5 };
|
||||
int target = 9;
|
||||
List<List<int>> res = subsetSumII(nums, target);
|
||||
List<List<int>> res = SubsetSumII(nums, target);
|
||||
Console.WriteLine("输入数组 nums = " + string.Join(", ", nums) + ", target = " + target);
|
||||
Console.WriteLine("所有和等于 " + target + " 的子集 res = ");
|
||||
foreach (var subset in res) {
|
||||
|
||||
Reference in New Issue
Block a user