mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-05 03:30:30 +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:
@@ -6,7 +6,7 @@ namespace hello_algo.chapter_array_and_linkedlist;
|
||||
|
||||
public class array {
|
||||
/* 随机访问元素 */
|
||||
public static int RandomAccess(int[] nums) {
|
||||
int RandomAccess(int[] nums) {
|
||||
Random random = new();
|
||||
// 在区间 [0, nums.Length) 中随机抽取一个数字
|
||||
int randomIndex = random.Next(nums.Length);
|
||||
@@ -16,7 +16,7 @@ public class array {
|
||||
}
|
||||
|
||||
/* 扩展数组长度 */
|
||||
public static int[] Extend(int[] nums, int enlarge) {
|
||||
int[] Extend(int[] nums, int enlarge) {
|
||||
// 初始化一个扩展长度后的数组
|
||||
int[] res = new int[nums.Length + enlarge];
|
||||
// 将原数组中的所有元素复制到新数组
|
||||
@@ -28,7 +28,7 @@ public class array {
|
||||
}
|
||||
|
||||
/* 在数组的索引 index 处插入元素 num */
|
||||
public static void Insert(int[] nums, int num, int index) {
|
||||
void Insert(int[] nums, int num, int index) {
|
||||
// 把索引 index 以及之后的所有元素向后移动一位
|
||||
for (int i = nums.Length - 1; i > index; i--) {
|
||||
nums[i] = nums[i - 1];
|
||||
@@ -38,7 +38,7 @@ public class array {
|
||||
}
|
||||
|
||||
/* 删除索引 index 处元素 */
|
||||
public static void Remove(int[] nums, int index) {
|
||||
void Remove(int[] nums, int index) {
|
||||
// 把索引 index 之后的所有元素向前移动一位
|
||||
for (int i = index; i < nums.Length - 1; i++) {
|
||||
nums[i] = nums[i + 1];
|
||||
@@ -46,7 +46,7 @@ public class array {
|
||||
}
|
||||
|
||||
/* 遍历数组 */
|
||||
public static void Traverse(int[] nums) {
|
||||
void Traverse(int[] nums) {
|
||||
int count = 0;
|
||||
// 通过索引遍历数组
|
||||
for (int i = 0; i < nums.Length; i++) {
|
||||
@@ -59,7 +59,7 @@ public class array {
|
||||
}
|
||||
|
||||
/* 在数组中查找指定元素 */
|
||||
public static int Find(int[] nums, int target) {
|
||||
int Find(int[] nums, int target) {
|
||||
for (int i = 0; i < nums.Length; i++) {
|
||||
if (nums[i] == target)
|
||||
return i;
|
||||
@@ -68,17 +68,17 @@ public class array {
|
||||
}
|
||||
|
||||
/* 辅助函数,数组转字符串 */
|
||||
public static string ToString(int[] nums) {
|
||||
string ToString(int[] nums) {
|
||||
return string.Join(",", nums);
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public static void Test() {
|
||||
public void Test() {
|
||||
// 初始化数组
|
||||
int[] arr = new int[5];
|
||||
Console.WriteLine("数组 arr = " + ToString(arr));
|
||||
int[] nums = { 1, 3, 2, 5, 4 };
|
||||
int[] nums = [1, 3, 2, 5, 4];
|
||||
Console.WriteLine("数组 nums = " + ToString(nums));
|
||||
|
||||
// 随机访问
|
||||
|
||||
@@ -6,14 +6,14 @@ namespace hello_algo.chapter_array_and_linkedlist;
|
||||
|
||||
public class linked_list {
|
||||
/* 在链表的节点 n0 之后插入节点 P */
|
||||
public static void Insert(ListNode n0, ListNode P) {
|
||||
void Insert(ListNode n0, ListNode P) {
|
||||
ListNode? n1 = n0.next;
|
||||
P.next = n1;
|
||||
n0.next = P;
|
||||
}
|
||||
|
||||
/* 删除链表的节点 n0 之后的首个节点 */
|
||||
public static void Remove(ListNode n0) {
|
||||
void Remove(ListNode n0) {
|
||||
if (n0.next == null)
|
||||
return;
|
||||
// n0 -> P -> n1
|
||||
@@ -23,7 +23,7 @@ public class linked_list {
|
||||
}
|
||||
|
||||
/* 访问链表中索引为 index 的节点 */
|
||||
public static ListNode? Access(ListNode head, int index) {
|
||||
ListNode? Access(ListNode? head, int index) {
|
||||
for (int i = 0; i < index; i++) {
|
||||
if (head == null)
|
||||
return null;
|
||||
@@ -33,7 +33,7 @@ public class linked_list {
|
||||
}
|
||||
|
||||
/* 在链表中查找值为 target 的首个节点 */
|
||||
public static int Find(ListNode head, int target) {
|
||||
int Find(ListNode? head, int target) {
|
||||
int index = 0;
|
||||
while (head != null) {
|
||||
if (head.val == target)
|
||||
|
||||
@@ -11,8 +11,8 @@ public class list {
|
||||
public void Test() {
|
||||
|
||||
/* 初始化列表 */
|
||||
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];
|
||||
Console.WriteLine("列表 nums = " + string.Join(",", nums));
|
||||
|
||||
/* 访问元素 */
|
||||
@@ -55,7 +55,7 @@ public class list {
|
||||
}
|
||||
|
||||
/* 拼接两个列表 */
|
||||
List<int> nums1 = new() { 6, 8, 7, 10, 9 };
|
||||
List<int> nums1 = [6, 8, 7, 10, 9];
|
||||
nums.AddRange(nums1);
|
||||
Console.WriteLine("将列表 nums1 拼接到 nums 之后,得到 nums = " + string.Join(",", nums));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user