From 92aa96d8f75942d47aa8e2711627c45e5ce9a906 Mon Sep 17 00:00:00 2001 From: Ming <37586974+mingXta@users.noreply.github.com> Date: Wed, 14 Dec 2022 11:03:28 +0800 Subject: [PATCH 1/6] Update array.md Add the C# code to docs ( Chapter of Array) Add the C# code to docs ( Chapter of Array) --- docs/chapter_array_and_linkedlist/array.md | 72 ++++++++++++++++++++-- 1 file changed, 67 insertions(+), 5 deletions(-) diff --git a/docs/chapter_array_and_linkedlist/array.md b/docs/chapter_array_and_linkedlist/array.md index caa7145b..d2b8a482 100644 --- a/docs/chapter_array_and_linkedlist/array.md +++ b/docs/chapter_array_and_linkedlist/array.md @@ -71,7 +71,11 @@ comments: true === "C#" ```csharp title="array.cs" + int[] arr = new int[5]; // { 0, 0, 0, 0, 0 } + int[] nums = { 1, 3, 2, 5, 4 }; + var arr2=new int[5]; // { 0, 0, 0, 0, 0 } + var nums2=new int[]{1,2,3,4,5}; ``` ## 数组优点 @@ -169,7 +173,14 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "C#" ```csharp title="array.cs" - + /* 随机返回一个数组元素 */ + int RandomAccess(int[] nums) + { + Random random=new(); + int randomIndex = random.Next(nums.Length); + int randomNum = nums[randomIndex]; + return randomNum; + } ``` ## 数组缺点 @@ -271,7 +282,18 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "C#" ```csharp title="array.cs" - + int[] Extend(int[] nums, int enlarge) + { + // 初始化一个扩展长度后的数组 + int[] res = new int[nums.Length + enlarge]; + // 将原数组中的所有元素复制到新数组 + for (int i = 0; i < nums.Length; i++) + { + res[i] = nums[i]; + } + // 返回扩展后的新数组 + return res; + } ``` **数组中插入或删除元素效率低下。** 假设我们想要在数组中间某位置插入一个元素,由于数组元素在内存中是 “紧挨着的” ,它们之间没有空间再放任何数据。因此,我们不得不将此索引之后的所有元素都向后移动一位,然后再把元素赋值给该索引。删除元素也是类似,需要把此索引之后的元素都向前移动一位。总体看有以下缺点: @@ -405,7 +427,24 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "C#" ```csharp title="array.cs" - + void Insert(int[] nums, int num, int index) + { + // 把索引 index 以及之后的所有元素向后移动一位 + for (int i = nums.Length - 1; i >= index; i--) + { + nums[i] = nums[i - 1]; + } + // 将 num 赋给 index 处元素 + nums[index] = num; + } + void Remove(int[] nums, int index) + { + // 把索引 index 之后的所有元素向前移动一位 + for (int i = index; i < nums.Length - 1; i++) + { + nums[i] = nums[i + 1]; + } + } ``` ## 数组常用操作 @@ -505,7 +544,21 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "C#" ```csharp title="array.cs" - + /* 遍历数组 */ + void Traverse(int[] nums) + { + int count = 0; + // 通过索引遍历数组 + for (int i = 0; i < nums.Length; i++) + { + count++; + } + // 直接遍历数组 + foreach (int num in nums) + { + count++; + } + } ``` **数组查找。** 通过遍历数组,查找数组内的指定元素,并输出对应索引。 @@ -588,7 +641,16 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "C#" ```csharp title="array.cs" - + /* 在数组中查找指定元素 */ + int Find(int[] nums, int target) + { + for (int i = 0; i < nums.Length; i++) + { + if (nums[i] == target) + return i; + } + return -1; + } ``` ## 数组典型应用 From d61196de3a0c591cede0e4a380baba0401bb3b77 Mon Sep 17 00:00:00 2001 From: Ming <37586974+mingXta@users.noreply.github.com> Date: Wed, 14 Dec 2022 11:22:14 +0800 Subject: [PATCH 2/6] Update array.md,fix md formatting errors Fix md formatting errors --- docs/chapter_array_and_linkedlist/array.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/chapter_array_and_linkedlist/array.md b/docs/chapter_array_and_linkedlist/array.md index d2b8a482..0c9f796a 100644 --- a/docs/chapter_array_and_linkedlist/array.md +++ b/docs/chapter_array_and_linkedlist/array.md @@ -641,7 +641,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "C#" ```csharp title="array.cs" - /* 在数组中查找指定元素 */ + /* 在数组中查找指定元素 */ int Find(int[] nums, int target) { for (int i = 0; i < nums.Length; i++) From 064d21a55d7e133e6ca605cacfd23913794f36ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=BD=A4?= <1195669834@qq.com> Date: Wed, 14 Dec 2022 13:54:49 +0800 Subject: [PATCH 3/6] Create Array.cs Add C# array code --- .../c#/chapter_array_and_linkedlist/Array.cs | 123 ++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 codes/c#/chapter_array_and_linkedlist/Array.cs diff --git a/codes/c#/chapter_array_and_linkedlist/Array.cs b/codes/c#/chapter_array_and_linkedlist/Array.cs new file mode 100644 index 00000000..5afabb46 --- /dev/null +++ b/codes/c#/chapter_array_and_linkedlist/Array.cs @@ -0,0 +1,123 @@ +namespace hello_algo.chapter_arrag_and_linkedlist +{ + public class Array + { + /* 随机返回一个数组元素 */ + + public static int RandomAccess(int[] nums) + { + Random random = new(); + int randomIndex = random.Next(nums.Length); + int randomNum = nums[randomIndex]; + return randomNum; + } + + /* 扩展数组长度 */ + + public static int[] Extend(int[] nums, int enlarge) + { + // 初始化一个扩展长度后的数组 + int[] res = new int[nums.Length + enlarge]; + // 将原数组中的所有元素复制到新数组 + for (int i = 0; i < nums.Length; i++) + { + res[i] = nums[i]; + } + // 返回扩展后的新数组 + return res; + } + + /* 在数组的索引 index 处插入元素 num */ + + public static void Insert(int[] nums, int num, int index) + { + // 把索引 index 以及之后的所有元素向后移动一位 + for (int i = nums.Length - 1; i >= index; i--) + { + nums[i] = nums[i - 1]; + } + // 将 num 赋给 index 处元素 + nums[index] = num; + } + + /* 删除索引 index 处元素 */ + + public static void Remove(int[] nums, int index) + { + // 把索引 index 之后的所有元素向前移动一位 + for (int i = index; i < nums.Length - 1; i++) + { + nums[i] = nums[i + 1]; + } + } + + /* 遍历数组 */ + + public static void Traverse(int[] nums) + { + int count = 0; + // 通过索引遍历数组 + for (int i = 0; i < nums.Length; i++) + { + count++; + } + // 直接遍历数组 + foreach (int num in nums) + { + count++; + } + } + + /* 在数组中查找指定元素 */ + + public static int Find(int[] nums, int target) + { + for (int i = 0; i < nums.Length; i++) + { + if (nums[i] == target) + return i; + } + return -1; + } + + /*辅助函数数组转字符串 */ + + public static string ToString(int[] nums) + { + return string.Join(",", nums); + } + + /* Driver Code */ + public static void Main() + { + /* 初始化数组 */ + int[] arr = new int[5]; + Console.WriteLine("数组 arr = " + ToString(arr)); + int[] nums = { 1, 3, 2, 5, 4 }; + Console.WriteLine("数组 nums = " + ToString(nums)); + + /* 随机访问 */ + int randomNum = RandomAccess(nums); + Console.WriteLine("在 nums 中获取随机元素 " + randomNum); + + /* 长度扩展 */ + nums = Extend(nums, 3); + Console.WriteLine("将数组长度扩展至 8 ,得到 nums = " + ToString(nums)); + + /* 插入元素 */ + Insert(nums, 6, 3); + Console.WriteLine("在索引 3 处插入数字 6 ,得到 nums = " + ToString(nums)); + + /* 删除元素 */ + Remove(nums, 2); + Console.WriteLine("删除索引 2 处的元素,得到 nums = " + ToString(nums)); + + /* 遍历数组 */ + Traverse(nums); + + /* 查找元素 */ + int index = Find(nums, 3); + Console.WriteLine("在 nums 中查找元素 3 ,得到索引 = " + index); + } + } +} \ No newline at end of file From 94f66d3f06557976c972bdad2203ca8c31f7abc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=BD=A4?= <1195669834@qq.com> Date: Wed, 14 Dec 2022 15:11:25 +0800 Subject: [PATCH 4/6] Update C# array code and doc Add some comments and make code specification --- codes/c#/chapter_array_and_linkedlist/Array.cs | 8 +++++++- docs/chapter_array_and_linkedlist/array.md | 10 +++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/codes/c#/chapter_array_and_linkedlist/Array.cs b/codes/c#/chapter_array_and_linkedlist/Array.cs index 5afabb46..7aaa64ac 100644 --- a/codes/c#/chapter_array_and_linkedlist/Array.cs +++ b/codes/c#/chapter_array_and_linkedlist/Array.cs @@ -1,4 +1,10 @@ -namespace hello_algo.chapter_arrag_and_linkedlist +/* + * File: Array.cs + * Created Time: 2022-12-14 + * Author: mingXta (1195669834@qq.com) + */ + +namespace hello_algo.chapter_arrag_and_linkedlist { public class Array { diff --git a/docs/chapter_array_and_linkedlist/array.md b/docs/chapter_array_and_linkedlist/array.md index 0c9f796a..39273163 100644 --- a/docs/chapter_array_and_linkedlist/array.md +++ b/docs/chapter_array_and_linkedlist/array.md @@ -71,6 +71,7 @@ comments: true === "C#" ```csharp title="array.cs" + /* 初始化数组 */ int[] arr = new int[5]; // { 0, 0, 0, 0, 0 } int[] nums = { 1, 3, 2, 5, 4 }; @@ -173,7 +174,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "C#" ```csharp title="array.cs" - /* 随机返回一个数组元素 */ + /* 随机返回一个数组元素 */ int RandomAccess(int[] nums) { Random random=new(); @@ -282,6 +283,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "C#" ```csharp title="array.cs" + /* 扩展数组长度 */ int[] Extend(int[] nums, int enlarge) { // 初始化一个扩展长度后的数组 @@ -386,7 +388,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex // 将 num 赋给 index 处元素 nums[index] = num; } - + /* 删除索引 index 处元素 */ function remove(nums, index) { // 把索引 index 之后的所有元素向前移动一位 @@ -427,6 +429,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "C#" ```csharp title="array.cs" + /* 在数组的索引 index 处插入元素 num */ void Insert(int[] nums, int num, int index) { // 把索引 index 以及之后的所有元素向后移动一位 @@ -437,6 +440,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex // 将 num 赋给 index 处元素 nums[index] = num; } + /* 删除索引 index 处元素 */ void Remove(int[] nums, int index) { // 把索引 index 之后的所有元素向前移动一位 @@ -544,7 +548,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "C#" ```csharp title="array.cs" - /* 遍历数组 */ + /* 遍历数组 */ void Traverse(int[] nums) { int count = 0; From f3430c059dc246f67060590171ed030ed7e49d28 Mon Sep 17 00:00:00 2001 From: Ming <37586974+mingXta@users.noreply.github.com> Date: Wed, 14 Dec 2022 19:42:39 +0800 Subject: [PATCH 5/6] Update Array.cs to remove the empty line --- .../c#/chapter_array_and_linkedlist/Array.cs | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/codes/c#/chapter_array_and_linkedlist/Array.cs b/codes/c#/chapter_array_and_linkedlist/Array.cs index 7aaa64ac..cb611505 100644 --- a/codes/c#/chapter_array_and_linkedlist/Array.cs +++ b/codes/c#/chapter_array_and_linkedlist/Array.cs @@ -1,15 +1,14 @@ -/* - * File: Array.cs - * Created Time: 2022-12-14 - * Author: mingXta (1195669834@qq.com) - */ +/* +* File: Array.cs +* Created Time: 2022-12-14 +* Author: mingXta (1195669834@qq.com) +*/ namespace hello_algo.chapter_arrag_and_linkedlist { public class Array { /* 随机返回一个数组元素 */ - public static int RandomAccess(int[] nums) { Random random = new(); @@ -19,7 +18,6 @@ namespace hello_algo.chapter_arrag_and_linkedlist } /* 扩展数组长度 */ - public static int[] Extend(int[] nums, int enlarge) { // 初始化一个扩展长度后的数组 @@ -34,7 +32,6 @@ namespace hello_algo.chapter_arrag_and_linkedlist } /* 在数组的索引 index 处插入元素 num */ - public static void Insert(int[] nums, int num, int index) { // 把索引 index 以及之后的所有元素向后移动一位 @@ -47,7 +44,6 @@ namespace hello_algo.chapter_arrag_and_linkedlist } /* 删除索引 index 处元素 */ - public static void Remove(int[] nums, int index) { // 把索引 index 之后的所有元素向前移动一位 @@ -58,7 +54,6 @@ namespace hello_algo.chapter_arrag_and_linkedlist } /* 遍历数组 */ - public static void Traverse(int[] nums) { int count = 0; @@ -75,7 +70,6 @@ namespace hello_algo.chapter_arrag_and_linkedlist } /* 在数组中查找指定元素 */ - public static int Find(int[] nums, int target) { for (int i = 0; i < nums.Length; i++) @@ -86,8 +80,7 @@ namespace hello_algo.chapter_arrag_and_linkedlist return -1; } - /*辅助函数数组转字符串 */ - + /*辅助函数,数组转字符串 */ public static string ToString(int[] nums) { return string.Join(",", nums); @@ -126,4 +119,4 @@ namespace hello_algo.chapter_arrag_and_linkedlist Console.WriteLine("在 nums 中查找元素 3 ,得到索引 = " + index); } } -} \ No newline at end of file +} From 8435fc69f9ec2df4207e78935b25923893fc1c8f Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Wed, 14 Dec 2022 23:04:56 +0800 Subject: [PATCH 6/6] Update Array.cs --- codes/c#/chapter_array_and_linkedlist/Array.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codes/c#/chapter_array_and_linkedlist/Array.cs b/codes/c#/chapter_array_and_linkedlist/Array.cs index cb611505..8f836a4f 100644 --- a/codes/c#/chapter_array_and_linkedlist/Array.cs +++ b/codes/c#/chapter_array_and_linkedlist/Array.cs @@ -4,7 +4,7 @@ * Author: mingXta (1195669834@qq.com) */ -namespace hello_algo.chapter_arrag_and_linkedlist +namespace hello_algo.chapter_array_and_linkedlist { public class Array {