mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-08 13:36:06 +00: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,7 +8,7 @@ namespace hello_algo.chapter_divide_and_conquer;
|
||||
|
||||
public class binary_search_recur {
|
||||
/* 二分查找:问题 f(i, j) */
|
||||
public int DFS(int[] nums, int target, int i, int j) {
|
||||
int DFS(int[] nums, int target, int i, int j) {
|
||||
// 若区间为空,代表无目标元素,则返回 -1
|
||||
if (i > j) {
|
||||
return -1;
|
||||
@@ -28,7 +28,7 @@ public class binary_search_recur {
|
||||
}
|
||||
|
||||
/* 二分查找 */
|
||||
public int BinarySearch(int[] nums, int target) {
|
||||
int BinarySearch(int[] nums, int target) {
|
||||
int n = nums.Length;
|
||||
// 求解问题 f(0, n-1)
|
||||
return DFS(nums, target, 0, n - 1);
|
||||
@@ -37,7 +37,7 @@ public class binary_search_recur {
|
||||
[Test]
|
||||
public void Test() {
|
||||
int target = 6;
|
||||
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 };
|
||||
int[] nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
|
||||
|
||||
// 二分查找(双闭区间)
|
||||
int index = BinarySearch(nums, target);
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_divide_and_conquer;
|
||||
|
||||
public class build_tree {
|
||||
/* 构建二叉树:分治 */
|
||||
public TreeNode DFS(int[] preorder, Dictionary<int, int> inorderMap, int i, int l, int r) {
|
||||
TreeNode? DFS(int[] preorder, Dictionary<int, int> inorderMap, int i, int l, int r) {
|
||||
// 子树区间为空时终止
|
||||
if (r - l < 0)
|
||||
return null;
|
||||
@@ -25,24 +25,24 @@ public class build_tree {
|
||||
}
|
||||
|
||||
/* 构建二叉树 */
|
||||
public TreeNode BuildTree(int[] preorder, int[] inorder) {
|
||||
TreeNode? BuildTree(int[] preorder, int[] inorder) {
|
||||
// 初始化哈希表,存储 inorder 元素到索引的映射
|
||||
Dictionary<int, int> inorderMap = new();
|
||||
Dictionary<int, int> inorderMap = [];
|
||||
for (int i = 0; i < inorder.Length; i++) {
|
||||
inorderMap.TryAdd(inorder[i], i);
|
||||
}
|
||||
TreeNode root = DFS(preorder, inorderMap, 0, 0, inorder.Length - 1);
|
||||
TreeNode? root = DFS(preorder, inorderMap, 0, 0, inorder.Length - 1);
|
||||
return root;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] preorder = { 3, 9, 2, 1, 7 };
|
||||
int[] inorder = { 9, 3, 1, 2, 7 };
|
||||
int[] preorder = [3, 9, 2, 1, 7];
|
||||
int[] inorder = [9, 3, 1, 2, 7];
|
||||
Console.WriteLine("前序遍历 = " + string.Join(", ", preorder));
|
||||
Console.WriteLine("中序遍历 = " + string.Join(", ", inorder));
|
||||
|
||||
TreeNode root = BuildTree(preorder, inorder);
|
||||
TreeNode? root = BuildTree(preorder, inorder);
|
||||
Console.WriteLine("构建的二叉树为:");
|
||||
PrintUtil.PrintTree(root);
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_divide_and_conquer;
|
||||
|
||||
public class hanota {
|
||||
/* 移动一个圆盘 */
|
||||
public void Move(List<int> src, List<int> tar) {
|
||||
void Move(List<int> src, List<int> tar) {
|
||||
// 从 src 顶部拿出一个圆盘
|
||||
int pan = src[^1];
|
||||
src.RemoveAt(src.Count - 1);
|
||||
@@ -17,7 +17,7 @@ public class hanota {
|
||||
}
|
||||
|
||||
/* 求解汉诺塔:问题 f(i) */
|
||||
public void DFS(int i, List<int> src, List<int> buf, List<int> tar) {
|
||||
void DFS(int i, List<int> src, List<int> buf, List<int> tar) {
|
||||
// 若 src 只剩下一个圆盘,则直接将其移到 tar
|
||||
if (i == 1) {
|
||||
Move(src, tar);
|
||||
@@ -32,7 +32,7 @@ public class hanota {
|
||||
}
|
||||
|
||||
/* 求解汉诺塔 */
|
||||
public void SolveHanota(List<int> A, List<int> B, List<int> C) {
|
||||
void SolveHanota(List<int> A, List<int> B, List<int> C) {
|
||||
int n = A.Count;
|
||||
// 将 A 顶部 n 个圆盘借助 B 移到 C
|
||||
DFS(n, A, B, C);
|
||||
@@ -41,9 +41,9 @@ public class hanota {
|
||||
[Test]
|
||||
public void Test() {
|
||||
// 列表尾部是柱子顶部
|
||||
List<int> A = new() { 5, 4, 3, 2, 1 };
|
||||
List<int> B = new();
|
||||
List<int> C = new();
|
||||
List<int> A = [5, 4, 3, 2, 1];
|
||||
List<int> B = [];
|
||||
List<int> C = [];
|
||||
Console.WriteLine("初始状态下:");
|
||||
Console.WriteLine("A = " + string.Join(", ", A));
|
||||
Console.WriteLine("B = " + string.Join(", ", B));
|
||||
|
||||
Reference in New Issue
Block a user