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:
hpstory
2023-11-26 23:18:44 +08:00
committed by GitHub
parent d960c99a1f
commit 56b20eff36
93 changed files with 539 additions and 487 deletions
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_searching;
public class binary_search {
/* 二分查找(双闭区间) */
static int BinarySearch(int[] nums, int target) {
int BinarySearch(int[] nums, int target) {
// 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素
int i = 0, j = nums.Length - 1;
// 循环,当搜索区间为空时跳出(当 i > j 时为空)
@@ -26,7 +26,7 @@ public class binary_search {
}
/* 二分查找(左闭右开) */
static int BinarySearchLCRO(int[] nums, int target) {
int BinarySearchLCRO(int[] nums, int target) {
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
int i = 0, j = nums.Length;
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
@@ -46,7 +46,7 @@ public class binary_search {
[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_searching;
public class binary_search_edge {
/* 二分查找最左一个 target */
public int BinarySearchLeftEdge(int[] nums, int target) {
int BinarySearchLeftEdge(int[] nums, int target) {
// 等价于查找 target 的插入点
int i = binary_search_insertion.BinarySearchInsertion(nums, target);
// 未找到 target ,返回 -1
@@ -20,7 +20,7 @@ public class binary_search_edge {
}
/* 二分查找最右一个 target */
public int BinarySearchRightEdge(int[] nums, int target) {
int BinarySearchRightEdge(int[] nums, int target) {
// 转化为查找最左一个 target + 1
int i = binary_search_insertion.BinarySearchInsertion(nums, target + 1);
// j 指向最右一个 target ,i 指向首个大于 target 的元素
@@ -36,7 +36,7 @@ public class binary_search_edge {
[Test]
public void Test() {
// 包含重复元素的数组
int[] nums = { 1, 3, 6, 6, 6, 6, 6, 10, 12, 15 };
int[] nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
Console.WriteLine("\n数组 nums = " + nums.PrintList());
// 二分查找左边界和右边界
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_searching;
public class binary_search_insertion {
/* 二分查找插入点(无重复元素) */
public int BinarySearchInsertionSimple(int[] nums, int target) {
public static int BinarySearchInsertionSimple(int[] nums, int target) {
int i = 0, j = nums.Length - 1; // 初始化双闭区间 [0, n-1]
while (i <= j) {
int m = i + (j - i) / 2; // 计算中点索引 m
@@ -44,7 +44,7 @@ public class binary_search_insertion {
[Test]
public void Test() {
// 无重复元素的数组
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 };
int[] nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
Console.WriteLine("\n数组 nums = " + nums.PrintList());
// 二分查找插入点
foreach (int target in new int[] { 6, 9 }) {
@@ -53,7 +53,7 @@ public class binary_search_insertion {
}
// 包含重复元素的数组
nums = new int[] { 1, 3, 6, 6, 6, 6, 6, 10, 12, 15 };
nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
Console.WriteLine("\n数组 nums = " + nums.PrintList());
// 二分查找插入点
foreach (int target in new int[] { 2, 6, 20 }) {
@@ -8,14 +8,14 @@ namespace hello_algo.chapter_searching;
public class hashing_search {
/* 哈希查找(数组) */
static int HashingSearchArray(Dictionary<int, int> map, int target) {
int HashingSearchArray(Dictionary<int, int> map, int target) {
// 哈希表的 key: 目标元素,value: 索引
// 若哈希表中无此 key ,返回 -1
return map.GetValueOrDefault(target, -1);
}
/* 哈希查找(链表) */
static ListNode? HashingSearchLinkedList(Dictionary<int, ListNode> map, int target) {
ListNode? HashingSearchLinkedList(Dictionary<int, ListNode> map, int target) {
// 哈希表的 key: 目标节点值,value: 节点对象
// 若哈希表中无此 key ,返回 null
@@ -27,9 +27,9 @@ public class hashing_search {
int target = 3;
/* 哈希查找(数组) */
int[] nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 };
int[] nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8];
// 初始化哈希表
Dictionary<int, int> map = new();
Dictionary<int, int> map = [];
for (int i = 0; i < nums.Length; i++) {
map[nums[i]] = i; // key: 元素,value: 索引
}
@@ -39,7 +39,7 @@ public class hashing_search {
/* 哈希查找(链表) */
ListNode? head = ListNode.ArrToLinkedList(nums);
// 初始化哈希表
Dictionary<int, ListNode> map1 = new();
Dictionary<int, ListNode> map1 = [];
while (head != null) {
map1[head.val] = head; // key: 节点值,value: 节点
head = head.next;
@@ -8,7 +8,7 @@ namespace hello_algo.chapter_searching;
public class linear_search {
/* 线性查找(数组) */
static int LinearSearchArray(int[] nums, int target) {
int LinearSearchArray(int[] nums, int target) {
// 遍历数组
for (int i = 0; i < nums.Length; i++) {
// 找到目标元素,返回其索引
@@ -20,7 +20,7 @@ public class linear_search {
}
/* 线性查找(链表) */
static ListNode? LinearSearchLinkedList(ListNode? head, int target) {
ListNode? LinearSearchLinkedList(ListNode? head, int target) {
// 遍历链表
while (head != null) {
// 找到目标节点,返回之
@@ -37,7 +37,7 @@ public class linear_search {
int target = 3;
/* 在数组中执行线性查找 */
int[] nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 };
int[] nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8];
int index = LinearSearchArray(nums, target);
Console.WriteLine("目标元素 3 的索引 = " + index);
+8 -8
View File
@@ -8,37 +8,37 @@ namespace hello_algo.chapter_searching;
public class two_sum {
/* 方法一:暴力枚举 */
public static int[] TwoSumBruteForce(int[] nums, int target) {
int[] TwoSumBruteForce(int[] nums, int target) {
int size = nums.Length;
// 两层循环,时间复杂度 O(n^2)
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (nums[i] + nums[j] == target)
return new int[] { i, j };
return [i, j];
}
}
return Array.Empty<int>();
return [];
}
/* 方法二:辅助哈希表 */
public static int[] TwoSumHashTable(int[] nums, int target) {
int[] TwoSumHashTable(int[] nums, int target) {
int size = nums.Length;
// 辅助哈希表,空间复杂度 O(n)
Dictionary<int, int> dic = new();
Dictionary<int, int> dic = [];
// 单层循环,时间复杂度 O(n)
for (int i = 0; i < size; i++) {
if (dic.ContainsKey(target - nums[i])) {
return new int[] { dic[target - nums[i]], i };
return [dic[target - nums[i]], i];
}
dic.Add(nums[i], i);
}
return Array.Empty<int>();
return [];
}
[Test]
public void Test() {
// ======= Test Case =======
int[] nums = { 2, 7, 11, 15 };
int[] nums = [2, 7, 11, 15];
int target = 13;
// ====== Driver Code ======