diff --git a/codes/csharp/chapter_array_and_linkedlist/list.cs b/codes/csharp/chapter_array_and_linkedlist/list.cs index 1a95c234..4df9b4c6 100644 --- a/codes/csharp/chapter_array_and_linkedlist/list.cs +++ b/codes/csharp/chapter_array_and_linkedlist/list.cs @@ -45,7 +45,7 @@ public class list { /* 通过索引遍历列表 */ int count = 0; - for (int i = 0; i < list.Count(); i++) { + for (int i = 0; i < list.Count; i++) { count++; } diff --git a/codes/csharp/chapter_array_and_linkedlist/my_list.cs b/codes/csharp/chapter_array_and_linkedlist/my_list.cs index 0709cb68..815fbc68 100644 --- a/codes/csharp/chapter_array_and_linkedlist/my_list.cs +++ b/codes/csharp/chapter_array_and_linkedlist/my_list.cs @@ -87,7 +87,7 @@ class MyList { /* 列表扩容 */ public void extendCapacity() { // 新建一个长度为 numsCapacity * extendRatio 的数组,并将原数组拷贝到新数组 - System.Array.Resize(ref nums, numsCapacity * extendRatio); + Array.Resize(ref nums, numsCapacity * extendRatio); // 更新列表容量 numsCapacity = nums.Length; } diff --git a/codes/csharp/chapter_computational_complexity/time_complexity.cs b/codes/csharp/chapter_computational_complexity/time_complexity.cs index 3e7c68e1..88fd84cb 100644 --- a/codes/csharp/chapter_computational_complexity/time_complexity.cs +++ b/codes/csharp/chapter_computational_complexity/time_complexity.cs @@ -87,9 +87,7 @@ public class time_complexity { for (int j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] - int tmp = nums[j]; - nums[j] = nums[j + 1]; - nums[j + 1] = tmp; + (nums[j + 1], nums[j]) = (nums[j], nums[j + 1]); count += 3; // 元素交换包含 3 个单元操作 } } diff --git a/codes/csharp/chapter_searching/two_sum.cs b/codes/csharp/chapter_searching/two_sum.cs index 85cae25b..0b2ff3e1 100644 --- a/codes/csharp/chapter_searching/two_sum.cs +++ b/codes/csharp/chapter_searching/two_sum.cs @@ -17,7 +17,7 @@ public class two_sum { return new int[] { i, j }; } } - return new int[0]; + return Array.Empty(); } /* 方法二:辅助哈希表 */ @@ -32,7 +32,7 @@ public class two_sum { } dic.Add(nums[i], i); } - return new int[0]; + return Array.Empty(); } [Test] diff --git a/codes/csharp/chapter_stack_and_queue/queue.cs b/codes/csharp/chapter_stack_and_queue/queue.cs index 121a0aa6..b42ac8f7 100644 --- a/codes/csharp/chapter_stack_and_queue/queue.cs +++ b/codes/csharp/chapter_stack_and_queue/queue.cs @@ -29,11 +29,11 @@ public class queue { Console.WriteLine("出队元素 pop = " + pop + ",出队后 queue = " + string.Join(",", queue)); /* 获取队列的长度 */ - int size = queue.Count(); + int size = queue.Count; Console.WriteLine("队列长度 size = " + size); /* 判断队列是否为空 */ - bool isEmpty = queue.Count() == 0; + bool isEmpty = queue.Count == 0; Console.WriteLine("队列是否为空 = " + isEmpty); } } diff --git a/codes/csharp/chapter_stack_and_queue/stack.cs b/codes/csharp/chapter_stack_and_queue/stack.cs index 0c86c4fe..7c2f827f 100644 --- a/codes/csharp/chapter_stack_and_queue/stack.cs +++ b/codes/csharp/chapter_stack_and_queue/stack.cs @@ -30,11 +30,11 @@ public class stack { Console.WriteLine("出栈元素 pop = " + pop + ",出栈后 stack = " + string.Join(",", stack)); /* 获取栈的长度 */ - int size = stack.Count(); + int size = stack.Count; Console.WriteLine("栈的长度 size = " + size); /* 判断是否为空 */ - bool isEmpty = stack.Count() == 0; + bool isEmpty = stack.Count == 0; Console.WriteLine("栈是否为空 = " + isEmpty); } } diff --git a/docs/chapter_array_and_linkedlist/linked_list.md b/docs/chapter_array_and_linkedlist/linked_list.md index 8de5a725..e701421d 100755 --- a/docs/chapter_array_and_linkedlist/linked_list.md +++ b/docs/chapter_array_and_linkedlist/linked_list.md @@ -111,8 +111,7 @@ ```csharp title="" /* 链表节点类 */ - class ListNode - { + class ListNode { int val; // 节点值 ListNode next; // 指向下一节点的引用 ListNode(int x) => val = x; //构造函数 diff --git a/docs/chapter_array_and_linkedlist/list.md b/docs/chapter_array_and_linkedlist/list.md index cb39bc8b..021011e5 100755 --- a/docs/chapter_array_and_linkedlist/list.md +++ b/docs/chapter_array_and_linkedlist/list.md @@ -539,15 +539,13 @@ ```csharp title="list.cs" /* 通过索引遍历列表 */ int count = 0; - for (int i = 0; i < list.Count(); i++) - { + for (int i = 0; i < list.Count; i++) { count++; } /* 直接遍历列表元素 */ count = 0; - foreach (int n in list) - { + foreach (int n in list) { count++; } ``` diff --git a/docs/chapter_computational_complexity/space_complexity.md b/docs/chapter_computational_complexity/space_complexity.md index 725c2691..a82dbae7 100755 --- a/docs/chapter_computational_complexity/space_complexity.md +++ b/docs/chapter_computational_complexity/space_complexity.md @@ -200,22 +200,19 @@ ```csharp title="" /* 类 */ - class Node - { + class Node { int val; Node next; Node(int x) { val = x; } } /* 函数 */ - int function() - { + int function() { // do something... return 0; } - int algorithm(int n) // 输入数据 - { + int algorithm(int n) { // 输入数据 const int a = 0; // 暂存数据(常量) int b = 0; // 暂存数据(变量) Node node = new Node(0); // 暂存数据(对象) @@ -376,12 +373,10 @@ === "C#" ```csharp title="" - void algorithm(int n) - { + void algorithm(int n) { int a = 0; // O(1) int[] b = new int[10000]; // O(1) - if (n > 10) - { + if (n > 10) { int[] nums = new int[n]; // O(n) } } @@ -564,22 +559,18 @@ === "C#" ```csharp title="" - int function() - { + int function() { // do something return 0; } /* 循环 O(1) */ - void loop(int n) - { - for (int i = 0; i < n; i++) - { + void loop(int n) { + for (int i = 0; i < n; i++) { function(); } } /* 递归 O(n) */ - int recur(int n) - { + int recur(int n) { if (n == 1) return 1; return recur(n - 1); } diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index 761b694c..8cffd92f 100755 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -62,12 +62,12 @@ $$ ```go title="" // 在某运行平台下 func algorithm(n int) { - a := 2 // 1 ns - a = a + 1 // 1 ns - a = a * 2 // 10 ns + a := 2 // 1 ns + a = a + 1 // 1 ns + a = a * 2 // 10 ns // 循环 n 次 - for i := 0; i < n; i++ { // 1 ns - fmt.Println(a) // 5 ns + for i := 0; i < n; i++ { // 1 ns + fmt.Println(a) // 5 ns } } ``` @@ -121,15 +121,13 @@ $$ ```csharp title="" // 在某运行平台下 - void algorithm(int n) - { + void algorithm(int n) { int a = 2; // 1 ns a = a + 1; // 1 ns a = a * 2; // 10 ns // 循环 n 次 - for (int i = 0; i < n; i++) - { // 1 ns ,每轮都要执行 i++ - Console.WriteLine(0); // 5 ns + for (int i = 0; i < n; i++) { // 1 ns ,每轮都要执行 i++ + Console.WriteLine(0); // 5 ns } } ``` @@ -329,23 +327,18 @@ $$ ```csharp title="" // 算法 A 时间复杂度:常数阶 - void algorithm_A(int n) - { + void algorithm_A(int n) { Console.WriteLine(0); } // 算法 B 时间复杂度:线性阶 - void algorithm_B(int n) - { - for (int i = 0; i < n; i++) - { + void algorithm_B(int n) { + for (int i = 0; i < n; i++) { Console.WriteLine(0); } } // 算法 C 时间复杂度:常数阶 - void algorithm_C(int n) - { - for (int i = 0; i < 1000000; i++) - { + void algorithm_C(int n) { + for (int i = 0; i < 1000000; i++) { Console.WriteLine(0); } } @@ -518,14 +511,12 @@ $$ === "C#" ```csharp title="" - void algorithm(int n) - { + void algorithm(int n) { int a = 1; // +1 a = a + 1; // +1 a = a * 2; // +1 // 循环 n 次 - for (int i = 0; i < n; i++) // +1(每轮都执行 i ++) - { + for (int i = 0; i < n; i++) { // +1(每轮都执行 i ++) Console.WriteLine(0); // +1 } } @@ -744,20 +735,16 @@ $$ === "C#" ```csharp title="" - void algorithm(int n) - { + void algorithm(int n) { int a = 1; // +0(技巧 1) a = a + n; // +0(技巧 1) // +n(技巧 2) - for (int i = 0; i < 5 * n + 1; i++) - { + for (int i = 0; i < 5 * n + 1; i++) { Console.WriteLine(0); } // +n*n(技巧 3) - for (int i = 0; i < 2 * n; i++) - { - for (int j = 0; j < n + 1; j++) - { + for (int i = 0; i < 2 * n; i++) { + for (int j = 0; j < n + 1; j++) { Console.WriteLine(0); } } diff --git a/docs/chapter_hashing/hash_map.md b/docs/chapter_hashing/hash_map.md index d69a707b..b1ffa995 100755 --- a/docs/chapter_hashing/hash_map.md +++ b/docs/chapter_hashing/hash_map.md @@ -368,18 +368,15 @@ ```csharp title="hash_map.cs" /* 遍历哈希表 */ // 遍历键值对 Key->Value - foreach (var kv in map) - { + foreach (var kv in map) { Console.WriteLine(kv.Key + " -> " + kv.Value); } // 单独遍历键 key - foreach (int key in map.Keys) - { + foreach (int key in map.Keys) { Console.WriteLine(key); } // 单独遍历值 value - foreach (String val in map.Values) - { + foreach (String val in map.Values) { Console.WriteLine(val); } ``` diff --git a/docs/chapter_stack_and_queue/queue.md b/docs/chapter_stack_and_queue/queue.md index e1a720b5..1ce5fe22 100755 --- a/docs/chapter_stack_and_queue/queue.md +++ b/docs/chapter_stack_and_queue/queue.md @@ -212,10 +212,10 @@ int pop = queue.Dequeue(); /* 获取队列的长度 */ - int size = queue.Count(); + int size = queue.Count; /* 判断队列是否为空 */ - bool isEmpty = queue.Count() == 0; + bool isEmpty = queue.Count == 0; ``` === "Swift" diff --git a/docs/chapter_stack_and_queue/stack.md b/docs/chapter_stack_and_queue/stack.md index 1c6b6d20..27abe997 100755 --- a/docs/chapter_stack_and_queue/stack.md +++ b/docs/chapter_stack_and_queue/stack.md @@ -211,10 +211,10 @@ int pop = stack.Pop(); /* 获取栈的长度 */ - int size = stack.Count(); + int size = stack.Count; /* 判断是否为空 */ - bool isEmpty = stack.Count()==0; + bool isEmpty = stack.Count == 0; ``` === "Swift"