mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-05 11:41:22 +08:00
feat(csharp) .NET 8.0 code migration (#966)
* .net 8.0 migration * update docs * revert change * revert change and update appendix docs * remove static * Update binary_search_insertion.cs * Update binary_search_insertion.cs * Update binary_search_edge.cs * Update binary_search_insertion.cs * Update binary_search_edge.cs --------- Co-authored-by: Yudong Jin <krahets@163.com>
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,
|
||||
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>> copyState = [];
|
||||
foreach (List<string> sRow in state) {
|
||||
copyState.Add(new List<string>(sRow));
|
||||
}
|
||||
@@ -39,11 +39,11 @@ public class n_queens {
|
||||
}
|
||||
|
||||
/* 求解 N 皇后 */
|
||||
static 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>> state = [];
|
||||
for (int i = 0; i < n; i++) {
|
||||
List<string> row = new();
|
||||
List<string> row = [];
|
||||
for (int j = 0; j < n; j++) {
|
||||
row.Add("#");
|
||||
}
|
||||
@@ -52,7 +52,7 @@ 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>>> res = [];
|
||||
|
||||
Backtrack(0, n, state, res, cols, diags1, diags2);
|
||||
|
||||
|
||||
@@ -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) {
|
||||
void Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {
|
||||
// 当状态长度等于元素数量时,记录解
|
||||
if (state.Count == choices.Length) {
|
||||
res.Add(new List<int>(state));
|
||||
@@ -32,15 +32,15 @@ public class permutations_i {
|
||||
}
|
||||
|
||||
/* 全排列 I */
|
||||
static List<List<int>> PermutationsI(int[] nums) {
|
||||
List<List<int>> res = new();
|
||||
Backtrack(new List<int>(), nums, new bool[nums.Length], res);
|
||||
List<List<int>> PermutationsI(int[] nums) {
|
||||
List<List<int>> res = [];
|
||||
Backtrack([], nums, new bool[nums.Length], res);
|
||||
return res;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = { 1, 2, 3 };
|
||||
int[] nums = [1, 2, 3];
|
||||
|
||||
List<List<int>> res = PermutationsI(nums);
|
||||
|
||||
|
||||
@@ -8,14 +8,14 @@ namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class permutations_ii {
|
||||
/* 回溯算法:全排列 II */
|
||||
static 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));
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
ISet<int> duplicated = new HashSet<int>();
|
||||
HashSet<int> duplicated = [];
|
||||
for (int i = 0; i < choices.Length; i++) {
|
||||
int choice = choices[i];
|
||||
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||
@@ -34,15 +34,15 @@ public class permutations_ii {
|
||||
}
|
||||
|
||||
/* 全排列 II */
|
||||
static List<List<int>> PermutationsII(int[] nums) {
|
||||
List<List<int>> res = new();
|
||||
Backtrack(new List<int>(), nums, new bool[nums.Length], res);
|
||||
List<List<int>> PermutationsII(int[] nums) {
|
||||
List<List<int>> res = [];
|
||||
Backtrack([], nums, new bool[nums.Length], res);
|
||||
return res;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = { 1, 2, 2 };
|
||||
int[] nums = [1, 2, 2];
|
||||
|
||||
List<List<int>> res = PermutationsII(nums);
|
||||
|
||||
|
||||
@@ -7,10 +7,10 @@
|
||||
namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class preorder_traversal_i_compact {
|
||||
static List<TreeNode> res;
|
||||
List<TreeNode> res = [];
|
||||
|
||||
/* 前序遍历:例题一 */
|
||||
static void PreOrder(TreeNode root) {
|
||||
void PreOrder(TreeNode? root) {
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
@@ -24,12 +24,11 @@ public class preorder_traversal_i_compact {
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
TreeNode root = TreeNode.ListToTree(new List<int?> { 1, 7, 3, 4, 5, 6, 7 });
|
||||
TreeNode? root = TreeNode.ListToTree([1, 7, 3, 4, 5, 6, 7]);
|
||||
Console.WriteLine("\n初始化二叉树");
|
||||
PrintUtil.PrintTree(root);
|
||||
|
||||
// 前序遍历
|
||||
res = new List<TreeNode>();
|
||||
PreOrder(root);
|
||||
|
||||
Console.WriteLine("\n输出所有值为 7 的节点");
|
||||
|
||||
@@ -7,11 +7,11 @@
|
||||
namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class preorder_traversal_ii_compact {
|
||||
static List<TreeNode> path;
|
||||
static List<List<TreeNode>> res;
|
||||
List<TreeNode> path = [];
|
||||
List<List<TreeNode>> res = [];
|
||||
|
||||
/* 前序遍历:例题二 */
|
||||
static void PreOrder(TreeNode root) {
|
||||
void PreOrder(TreeNode? root) {
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
@@ -29,13 +29,11 @@ public class preorder_traversal_ii_compact {
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
TreeNode root = TreeNode.ListToTree(new List<int?> { 1, 7, 3, 4, 5, 6, 7 });
|
||||
TreeNode? root = TreeNode.ListToTree([1, 7, 3, 4, 5, 6, 7]);
|
||||
Console.WriteLine("\n初始化二叉树");
|
||||
PrintUtil.PrintTree(root);
|
||||
|
||||
// 前序遍历
|
||||
path = new List<TreeNode>();
|
||||
res = new List<List<TreeNode>>();
|
||||
PreOrder(root);
|
||||
|
||||
Console.WriteLine("\n输出所有根节点到节点 7 的路径");
|
||||
|
||||
@@ -7,11 +7,11 @@
|
||||
namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class preorder_traversal_iii_compact {
|
||||
static List<TreeNode> path;
|
||||
static List<List<TreeNode>> res;
|
||||
List<TreeNode> path = [];
|
||||
List<List<TreeNode>> res = [];
|
||||
|
||||
/* 前序遍历:例题三 */
|
||||
static void PreOrder(TreeNode root) {
|
||||
void PreOrder(TreeNode? root) {
|
||||
// 剪枝
|
||||
if (root == null || root.val == 3) {
|
||||
return;
|
||||
@@ -30,13 +30,11 @@ public class preorder_traversal_iii_compact {
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
TreeNode root = TreeNode.ListToTree(new List<int?> { 1, 7, 3, 4, 5, 6, 7 });
|
||||
TreeNode? root = TreeNode.ListToTree([1, 7, 3, 4, 5, 6, 7]);
|
||||
Console.WriteLine("\n初始化二叉树");
|
||||
PrintUtil.PrintTree(root);
|
||||
|
||||
// 前序遍历
|
||||
path = new List<TreeNode>();
|
||||
res = new List<List<TreeNode>>();
|
||||
PreOrder(root);
|
||||
|
||||
Console.WriteLine("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点");
|
||||
|
||||
@@ -8,32 +8,32 @@ namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class preorder_traversal_iii_template {
|
||||
/* 判断当前状态是否为解 */
|
||||
static bool IsSolution(List<TreeNode> state) {
|
||||
bool IsSolution(List<TreeNode> state) {
|
||||
return state.Count != 0 && state[^1].val == 7;
|
||||
}
|
||||
|
||||
/* 记录解 */
|
||||
static void RecordSolution(List<TreeNode> state, List<List<TreeNode>> res) {
|
||||
void RecordSolution(List<TreeNode> state, List<List<TreeNode>> res) {
|
||||
res.Add(new List<TreeNode>(state));
|
||||
}
|
||||
|
||||
/* 判断在当前状态下,该选择是否合法 */
|
||||
static bool IsValid(List<TreeNode> state, TreeNode choice) {
|
||||
bool IsValid(List<TreeNode> state, TreeNode choice) {
|
||||
return choice != null && choice.val != 3;
|
||||
}
|
||||
|
||||
/* 更新状态 */
|
||||
static void MakeChoice(List<TreeNode> state, TreeNode choice) {
|
||||
void MakeChoice(List<TreeNode> state, TreeNode choice) {
|
||||
state.Add(choice);
|
||||
}
|
||||
|
||||
/* 恢复状态 */
|
||||
static void UndoChoice(List<TreeNode> state, TreeNode choice) {
|
||||
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) {
|
||||
void Backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {
|
||||
// 检查是否为解
|
||||
if (IsSolution(state)) {
|
||||
// 记录解
|
||||
@@ -46,7 +46,7 @@ public class preorder_traversal_iii_template {
|
||||
// 尝试:做出选择,更新状态
|
||||
MakeChoice(state, choice);
|
||||
// 进行下一轮选择
|
||||
Backtrack(state, new List<TreeNode> { choice.left, choice.right }, res);
|
||||
Backtrack(state, [choice.left!, choice.right!], res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
UndoChoice(state, choice);
|
||||
}
|
||||
@@ -55,14 +55,14 @@ public class preorder_traversal_iii_template {
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
TreeNode root = TreeNode.ListToTree(new List<int?> { 1, 7, 3, 4, 5, 6, 7 });
|
||||
TreeNode? root = TreeNode.ListToTree([1, 7, 3, 4, 5, 6, 7]);
|
||||
Console.WriteLine("\n初始化二叉树");
|
||||
PrintUtil.PrintTree(root);
|
||||
|
||||
// 回溯算法
|
||||
List<List<TreeNode>> res = new();
|
||||
List<TreeNode> choices = new() { root };
|
||||
Backtrack(new List<TreeNode>(), choices, res);
|
||||
List<List<TreeNode>> res = [];
|
||||
List<TreeNode> choices = [root!];
|
||||
Backtrack([], 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) {
|
||||
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));
|
||||
@@ -32,18 +32,18 @@ public class subset_sum_i {
|
||||
}
|
||||
|
||||
/* 求解子集和 I */
|
||||
public static List<List<int>> SubsetSumI(int[] nums, int target) {
|
||||
List<int> state = new(); // 状态(子集)
|
||||
List<List<int>> SubsetSumI(int[] nums, int target) {
|
||||
List<int> state = []; // 状态(子集)
|
||||
Array.Sort(nums); // 对 nums 进行排序
|
||||
int start = 0; // 遍历起始点
|
||||
List<List<int>> res = new(); // 结果列表(子集列表)
|
||||
List<List<int>> res = []; // 结果列表(子集列表)
|
||||
Backtrack(state, target, nums, start, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = { 3, 4, 5 };
|
||||
int[] nums = [3, 4, 5];
|
||||
int target = 9;
|
||||
List<List<int>> res = SubsetSumI(nums, target);
|
||||
Console.WriteLine("输入数组 nums = " + string.Join(", ", nums) + ", target = " + target);
|
||||
|
||||
@@ -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) {
|
||||
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));
|
||||
@@ -30,17 +30,17 @@ public class subset_sum_i_naive {
|
||||
}
|
||||
|
||||
/* 求解子集和 I(包含重复子集) */
|
||||
public static List<List<int>> SubsetSumINaive(int[] nums, int target) {
|
||||
List<int> state = new(); // 状态(子集)
|
||||
List<List<int>> SubsetSumINaive(int[] nums, int target) {
|
||||
List<int> state = []; // 状态(子集)
|
||||
int total = 0; // 子集和
|
||||
List<List<int>> res = new(); // 结果列表(子集列表)
|
||||
List<List<int>> res = []; // 结果列表(子集列表)
|
||||
Backtrack(state, target, total, nums, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = { 3, 4, 5 };
|
||||
int[] nums = [3, 4, 5];
|
||||
int target = 9;
|
||||
List<List<int>> res = SubsetSumINaive(nums, target);
|
||||
Console.WriteLine("输入数组 nums = " + string.Join(", ", nums) + ", target = " + target);
|
||||
|
||||
@@ -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) {
|
||||
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));
|
||||
@@ -37,18 +37,18 @@ public class subset_sum_ii {
|
||||
}
|
||||
|
||||
/* 求解子集和 II */
|
||||
public static List<List<int>> SubsetSumII(int[] nums, int target) {
|
||||
List<int> state = new(); // 状态(子集)
|
||||
List<List<int>> SubsetSumII(int[] nums, int target) {
|
||||
List<int> state = []; // 状态(子集)
|
||||
Array.Sort(nums); // 对 nums 进行排序
|
||||
int start = 0; // 遍历起始点
|
||||
List<List<int>> res = new(); // 结果列表(子集列表)
|
||||
List<List<int>> res = []; // 结果列表(子集列表)
|
||||
Backtrack(state, target, nums, start, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = { 4, 4, 5 };
|
||||
int[] nums = [4, 4, 5];
|
||||
int target = 9;
|
||||
List<List<int>> res = SubsetSumII(nums, target);
|
||||
Console.WriteLine("输入数组 nums = " + string.Join(", ", nums) + ", target = " + target);
|
||||
|
||||
Reference in New Issue
Block a user