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:
@@ -5,14 +5,10 @@
|
||||
namespace hello_algo.utils;
|
||||
|
||||
/* Definition for a singly-linked list node */
|
||||
public class ListNode {
|
||||
public int val;
|
||||
public class ListNode(int x) {
|
||||
public int val = x;
|
||||
public ListNode? next;
|
||||
|
||||
public ListNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
|
||||
/* Generate a linked list with an array */
|
||||
public static ListNode? ArrToLinkedList(int[] arr) {
|
||||
ListNode dum = new(0);
|
||||
@@ -33,7 +29,7 @@ public class ListNode {
|
||||
}
|
||||
|
||||
public override string? ToString() {
|
||||
List<string> list = new();
|
||||
List<string> list = [];
|
||||
var head = this;
|
||||
while (head != null) {
|
||||
list.Add(head.val.ToString());
|
||||
|
||||
@@ -6,14 +6,9 @@
|
||||
|
||||
namespace hello_algo.utils;
|
||||
|
||||
public class Trunk {
|
||||
public Trunk? prev;
|
||||
public string str;
|
||||
|
||||
public Trunk(Trunk? prev, string str) {
|
||||
this.prev = prev;
|
||||
this.str = str;
|
||||
}
|
||||
public class Trunk(Trunk? prev, string str) {
|
||||
public Trunk? prev = prev;
|
||||
public string str = str;
|
||||
};
|
||||
|
||||
public static class PrintUtil {
|
||||
@@ -45,8 +40,8 @@ public static class PrintUtil {
|
||||
}
|
||||
|
||||
/* Print a linked list */
|
||||
public static void PrintLinkedList(ListNode head) {
|
||||
List<string> list = new();
|
||||
public static void PrintLinkedList(ListNode? head) {
|
||||
List<string> list = [];
|
||||
while (head != null) {
|
||||
list.Add(head.val.ToString());
|
||||
head = head.next;
|
||||
@@ -115,10 +110,10 @@ public static class PrintUtil {
|
||||
/* Print a heap */
|
||||
public static void PrintHeap(Queue<int> queue) {
|
||||
Console.Write("堆的数组表示:");
|
||||
List<int> list = queue.ToList();
|
||||
List<int> list = [.. queue];
|
||||
Console.WriteLine(string.Join(',', list));
|
||||
Console.WriteLine("堆的树状表示:");
|
||||
TreeNode tree = TreeNode.ListToTree(list.Cast<int?>().ToList());
|
||||
TreeNode? tree = TreeNode.ListToTree(list.Cast<int?>().ToList());
|
||||
PrintTree(tree);
|
||||
}
|
||||
|
||||
@@ -126,13 +121,13 @@ public static class PrintUtil {
|
||||
public static void PrintHeap(PriorityQueue<int, int> queue) {
|
||||
var newQueue = new PriorityQueue<int, int>(queue.UnorderedItems, queue.Comparer);
|
||||
Console.Write("堆的数组表示:");
|
||||
List<int> list = new();
|
||||
List<int> list = [];
|
||||
while (newQueue.TryDequeue(out int element, out _)) {
|
||||
list.Add(element);
|
||||
}
|
||||
Console.WriteLine("堆的树状表示:");
|
||||
Console.WriteLine(string.Join(',', list.ToList()));
|
||||
TreeNode tree = TreeNode.ListToTree(list.Cast<int?>().ToList());
|
||||
TreeNode? tree = TreeNode.ListToTree(list.Cast<int?>().ToList());
|
||||
PrintTree(tree);
|
||||
}
|
||||
}
|
||||
@@ -7,17 +7,12 @@
|
||||
namespace hello_algo.utils;
|
||||
|
||||
/* 二叉树节点类 */
|
||||
public class TreeNode {
|
||||
public int val; // 节点值
|
||||
public int height; // 节点高度
|
||||
public class TreeNode(int? x) {
|
||||
public int? val = x; // 节点值
|
||||
public int height; // 节点高度
|
||||
public TreeNode? left; // 左子节点引用
|
||||
public TreeNode? right; // 右子节点引用
|
||||
|
||||
/* 构造方法 */
|
||||
public TreeNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
|
||||
// 序列化编码规则请参考:
|
||||
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
// 二叉树的数组表示:
|
||||
@@ -35,11 +30,11 @@ public class TreeNode {
|
||||
// \——— 8
|
||||
|
||||
/* 将列表反序列化为二叉树:递归 */
|
||||
private static TreeNode? ListToTreeDFS(List<int?> arr, int i) {
|
||||
static TreeNode? ListToTreeDFS(List<int?> arr, int i) {
|
||||
if (i < 0 || i >= arr.Count || !arr[i].HasValue) {
|
||||
return null;
|
||||
}
|
||||
TreeNode root = new(arr[i].Value) {
|
||||
TreeNode root = new(arr[i]) {
|
||||
left = ListToTreeDFS(arr, 2 * i + 1),
|
||||
right = ListToTreeDFS(arr, 2 * i + 2)
|
||||
};
|
||||
@@ -52,7 +47,7 @@ public class TreeNode {
|
||||
}
|
||||
|
||||
/* 将二叉树序列化为列表:递归 */
|
||||
private static void TreeToListDFS(TreeNode? root, int i, List<int?> res) {
|
||||
static void TreeToListDFS(TreeNode? root, int i, List<int?> res) {
|
||||
if (root == null)
|
||||
return;
|
||||
while (i >= res.Count) {
|
||||
@@ -65,7 +60,7 @@ public class TreeNode {
|
||||
|
||||
/* 将二叉树序列化为列表 */
|
||||
public static List<int?> TreeToList(TreeNode root) {
|
||||
List<int?> res = new();
|
||||
List<int?> res = [];
|
||||
TreeToListDFS(root, 0, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -7,11 +7,8 @@
|
||||
namespace hello_algo.utils;
|
||||
|
||||
/* 顶点类 */
|
||||
public class Vertex {
|
||||
public int val;
|
||||
public Vertex(int val) {
|
||||
this.val = val;
|
||||
}
|
||||
public class Vertex(int val) {
|
||||
public int val = val;
|
||||
|
||||
/* 输入值列表 vals ,返回顶点列表 vets */
|
||||
public static Vertex[] ValsToVets(int[] vals) {
|
||||
@@ -24,7 +21,7 @@ public class Vertex {
|
||||
|
||||
/* 输入顶点列表 vets ,返回值列表 vals */
|
||||
public static List<int> VetsToVals(List<Vertex> vets) {
|
||||
List<int> vals = new();
|
||||
List<int> vals = [];
|
||||
foreach (Vertex vet in vets) {
|
||||
vals.Add(vet.val);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user