mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 18:00:18 +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:
@@ -35,7 +35,7 @@
|
||||
|
||||
### C# 环境
|
||||
|
||||
1. 下载并安装 [.Net 6.0](https://dotnet.microsoft.com/en-us/download) 。
|
||||
1. 下载并安装 [.Net 8.0](https://dotnet.microsoft.com/en-us/download) 。
|
||||
2. 在 VSCode 的插件市场中搜索 `C# Dev Kit` ,安装 C# Dev Kit ([配置教程](https://code.visualstudio.com/docs/csharp/get-started))。
|
||||
3. 也可使用 Visual Studio([安装教程](https://learn.microsoft.com/zh-cn/visualstudio/install/install-visual-studio?view=vs-2022))。
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@
|
||||
```csharp title="array.cs"
|
||||
/* 初始化数组 */
|
||||
int[] arr = new int[5]; // { 0, 0, 0, 0, 0 }
|
||||
int[] nums = { 1, 3, 2, 5, 4 };
|
||||
int[] nums = [1, 3, 2, 5, 4];
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
@@ -52,10 +52,9 @@
|
||||
|
||||
```csharp title=""
|
||||
/* 链表节点类 */
|
||||
class ListNode {
|
||||
int val; // 节点值
|
||||
ListNode next; // 指向下一节点的引用
|
||||
ListNode(int x) => val = x; //构造函数
|
||||
class ListNode(int x) { //构造函数
|
||||
int val = x; // 节点值
|
||||
ListNode? next; // 指向下一节点的引用
|
||||
}
|
||||
```
|
||||
|
||||
@@ -495,11 +494,10 @@
|
||||
|
||||
```csharp title=""
|
||||
/* 双向链表节点类 */
|
||||
class ListNode {
|
||||
int val; // 节点值
|
||||
class ListNode(int x) { // 构造函数
|
||||
int val = x; // 节点值
|
||||
ListNode next; // 指向后继节点的引用
|
||||
ListNode prev; // 指向前驱节点的引用
|
||||
ListNode(int x) => val = x; // 构造函数
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -54,10 +54,10 @@
|
||||
```csharp title="list.cs"
|
||||
/* 初始化列表 */
|
||||
// 无初始值
|
||||
List<int> nums1 = new();
|
||||
List<int> nums1 = [];
|
||||
// 有初始值
|
||||
int[] numbers = new int[] { 1, 3, 2, 5, 4 };
|
||||
List<int> nums = numbers.ToList();
|
||||
int[] numbers = [1, 3, 2, 5, 4];
|
||||
List<int> nums = [.. numbers];
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@@ -700,7 +700,7 @@
|
||||
|
||||
```csharp title="list.cs"
|
||||
/* 拼接两个列表 */
|
||||
List<int> nums1 = new() { 6, 8, 7, 10, 9 };
|
||||
List<int> nums1 = [6, 8, 7, 10, 9];
|
||||
nums.AddRange(nums1); // 将列表 nums1 拼接到 nums 之后
|
||||
```
|
||||
|
||||
|
||||
@@ -98,10 +98,9 @@
|
||||
|
||||
```csharp title=""
|
||||
/* 类 */
|
||||
class Node {
|
||||
int val;
|
||||
class Node(int x) {
|
||||
int val = x;
|
||||
Node next;
|
||||
Node(int x) { val = x; }
|
||||
}
|
||||
|
||||
/* 函数 */
|
||||
@@ -113,7 +112,7 @@
|
||||
int Algorithm(int n) { // 输入数据
|
||||
const int a = 0; // 暂存数据(常量)
|
||||
int b = 0; // 暂存数据(变量)
|
||||
Node node = new(0); // 暂存数据(对象)
|
||||
Node node = new(0); // 暂存数据(对象)
|
||||
int c = Function(); // 栈帧空间(调用函数)
|
||||
return a + b + c; // 输出数据
|
||||
}
|
||||
|
||||
@@ -211,7 +211,7 @@ $$
|
||||
int hashStr = str.GetHashCode();
|
||||
// 字符串 Hello 算法 的哈希值为 -586107568;
|
||||
|
||||
object[] arr = { 12836, "小哈" };
|
||||
object[] arr = [12836, "小哈"];
|
||||
int hashTup = arr.GetHashCode();
|
||||
// 数组 [12836, 小哈] 的哈希值为 42931033;
|
||||
|
||||
|
||||
@@ -187,7 +187,7 @@
|
||||
bool isEmpty = maxHeap.Count == 0;
|
||||
|
||||
/* 输入列表并建堆 */
|
||||
minHeap = new PriorityQueue<int, int>(new List<(int, int)> { (1, 1), (3, 3), (2, 2), (5, 5), (4, 4), });
|
||||
minHeap = new PriorityQueue<int, int>([(1, 1), (3, 3), (2, 2), (5, 5), (4, 4)]);
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
@@ -53,7 +53,7 @@
|
||||
```csharp title=""
|
||||
/* 二叉树的数组表示 */
|
||||
// 使用 int? 可空类型 ,就可以使用 null 来标记空位
|
||||
int?[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };
|
||||
int?[] tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
@@ -63,12 +63,11 @@ AVL 树既是二叉搜索树也是平衡二叉树,同时满足这两类二叉
|
||||
|
||||
```csharp title=""
|
||||
/* AVL 树节点类 */
|
||||
class TreeNode {
|
||||
public int val; // 节点值
|
||||
public int height; // 节点高度
|
||||
public TreeNode? left; // 左子节点
|
||||
public TreeNode? right; // 右子节点
|
||||
public TreeNode(int x) { val = x; }
|
||||
class TreeNode(int? x) {
|
||||
public int? val = x; // 节点值
|
||||
public int height; // 节点高度
|
||||
public TreeNode? left; // 左子节点引用
|
||||
public TreeNode? right; // 右子节点引用
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -41,11 +41,10 @@
|
||||
|
||||
```csharp title=""
|
||||
/* 二叉树节点类 */
|
||||
class TreeNode {
|
||||
int val; // 节点值
|
||||
TreeNode? left; // 左子节点引用
|
||||
TreeNode? right; // 右子节点引用
|
||||
TreeNode(int x) { val = x; }
|
||||
class TreeNode(int? x) {
|
||||
public int? val = x; // 节点值
|
||||
public TreeNode? left; // 左子节点引用
|
||||
public TreeNode? right; // 右子节点引用
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user