fix(csharp): Modify method name to PascalCase, simplify new expression (#840)

* Modify method name to PascalCase(array and linked list)

* Modify method name to PascalCase(backtracking)

* Modify method name to PascalCase(computational complexity)

* Modify method name to PascalCase(divide and conquer)

* Modify method name to PascalCase(dynamic programming)

* Modify method name to PascalCase(graph)

* Modify method name to PascalCase(greedy)

* Modify method name to PascalCase(hashing)

* Modify method name to PascalCase(heap)

* Modify method name to PascalCase(searching)

* Modify method name to PascalCase(sorting)

* Modify method name to PascalCase(stack and queue)

* Modify method name to PascalCase(tree)

* local check
This commit is contained in:
hpstory
2023-10-08 01:33:46 +08:00
committed by GitHub
parent 6f7e768cb7
commit f62256bee1
129 changed files with 1186 additions and 1192 deletions

View File

@ -6,7 +6,7 @@ namespace hello_algo.chapter_array_and_linkedlist;
public class array {
/* 随机访问元素 */
public static int randomAccess(int[] nums) {
public static 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) {
public static 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) {
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];
@ -38,7 +38,7 @@ public class array {
}
/* 删除索引 index 处元素 */
public static void remove(int[] nums, int index) {
public static 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) {
public static 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) {
public static int Find(int[] nums, int target) {
for (int i = 0; i < nums.Length; i++) {
if (nums[i] == target)
return i;
@ -68,7 +68,7 @@ public class array {
}
/* 辅助函数,数组转字符串 */
public static string toString(int[] nums) {
public static string ToString(int[] nums) {
return string.Join(",", nums);
}
@ -77,31 +77,31 @@ public class array {
public static void Test() {
// 初始化数组
int[] arr = new int[5];
Console.WriteLine("数组 arr = " + toString(arr));
Console.WriteLine("数组 arr = " + ToString(arr));
int[] nums = { 1, 3, 2, 5, 4 };
Console.WriteLine("数组 nums = " + toString(nums));
Console.WriteLine("数组 nums = " + ToString(nums));
// 随机访问
int randomNum = randomAccess(nums);
int randomNum = RandomAccess(nums);
Console.WriteLine("在 nums 中获取随机元素 " + randomNum);
// 长度扩展
nums = extend(nums, 3);
Console.WriteLine("将数组长度扩展至 8 ,得到 nums = " + toString(nums));
nums = Extend(nums, 3);
Console.WriteLine("将数组长度扩展至 8 ,得到 nums = " + ToString(nums));
// 插入元素
insert(nums, 6, 3);
Console.WriteLine("在索引 3 处插入数字 6 ,得到 nums = " + toString(nums));
Insert(nums, 6, 3);
Console.WriteLine("在索引 3 处插入数字 6 ,得到 nums = " + ToString(nums));
// 删除元素
remove(nums, 2);
Console.WriteLine("删除索引 2 处的元素,得到 nums = " + toString(nums));
Remove(nums, 2);
Console.WriteLine("删除索引 2 处的元素,得到 nums = " + ToString(nums));
// 遍历数组
traverse(nums);
Traverse(nums);
// 查找元素
int index = find(nums, 3);
int index = Find(nums, 3);
Console.WriteLine("在 nums 中查找元素 3 ,得到索引 = " + index);
}
}

View File

@ -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) {
public static void Insert(ListNode n0, ListNode P) {
ListNode? n1 = n0.next;
P.next = n1;
n0.next = P;
}
/* 删除链表的节点 n0 之后的首个节点 */
public static void remove(ListNode n0) {
public static 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) {
public static 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) {
public static int Find(ListNode head, int target) {
int index = 0;
while (head != null) {
if (head.val == target)
@ -49,11 +49,11 @@ public class linked_list {
public void Test() {
// 初始化链表
// 初始化各个节点
ListNode n0 = new ListNode(1);
ListNode n1 = new ListNode(3);
ListNode n2 = new ListNode(2);
ListNode n3 = new ListNode(5);
ListNode n4 = new ListNode(4);
ListNode n0 = new(1);
ListNode n1 = new(3);
ListNode n2 = new(2);
ListNode n3 = new(5);
ListNode n4 = new(4);
// 构建引用指向
n0.next = n1;
n1.next = n2;
@ -62,19 +62,19 @@ public class linked_list {
Console.WriteLine($"初始化的链表为{n0}");
// 插入节点
insert(n0, new ListNode(0));
Insert(n0, new ListNode(0));
Console.WriteLine($"插入节点后的链表为{n0}");
// 删除节点
remove(n0);
Remove(n0);
Console.WriteLine($"删除节点后的链表为{n0}");
// 访问节点
ListNode? node = access(n0, 3);
ListNode? node = Access(n0, 3);
Console.WriteLine($"链表中索引 3 处的节点的值 = {node?.val}");
// 查找节点
int index = find(n0, 2);
int index = Find(n0, 2);
Console.WriteLine($"链表中值为 2 的节点的索引 = {index}");
}
}

View File

@ -11,7 +11,7 @@ class MyList {
private int[] nums; // 数组(存储列表元素)
private int numsCapacity = 10; // 列表容量
private int numsSize = 0; // 列表长度(即当前元素数量)
private int extendRatio = 2; // 每次列表扩容的倍数
private readonly int extendRatio = 2; // 每次列表扩容的倍数
/* 构造方法 */
public MyList() {
@ -19,17 +19,17 @@ class MyList {
}
/* 获取列表长度(即当前元素数量)*/
public int size() {
public int Size() {
return numsSize;
}
/* 获取列表容量 */
public int capacity() {
public int Capacity() {
return numsCapacity;
}
/* 访问元素 */
public int get(int index) {
public int Get(int index) {
// 索引如果越界则抛出异常,下同
if (index < 0 || index >= numsSize)
throw new IndexOutOfRangeException("索引越界");
@ -37,29 +37,29 @@ class MyList {
}
/* 更新元素 */
public void set(int index, int num) {
public void Set(int index, int num) {
if (index < 0 || index >= numsSize)
throw new IndexOutOfRangeException("索引越界");
nums[index] = num;
}
/* 尾部添加元素 */
public void add(int num) {
public void Add(int num) {
// 元素数量超出容量时,触发扩容机制
if (numsSize == numsCapacity)
extendCapacity();
ExtendCapacity();
nums[numsSize] = num;
// 更新元素数量
numsSize++;
}
/* 中间插入元素 */
public void insert(int index, int num) {
public void Insert(int index, int num) {
if (index < 0 || index >= numsSize)
throw new IndexOutOfRangeException("索引越界");
// 元素数量超出容量时,触发扩容机制
if (numsSize == numsCapacity)
extendCapacity();
ExtendCapacity();
// 将索引 index 以及之后的元素都向后移动一位
for (int j = numsSize - 1; j >= index; j--) {
nums[j + 1] = nums[j];
@ -70,7 +70,7 @@ class MyList {
}
/* 删除元素 */
public int remove(int index) {
public int Remove(int index) {
if (index < 0 || index >= numsSize)
throw new IndexOutOfRangeException("索引越界");
int num = nums[index];
@ -85,7 +85,7 @@ class MyList {
}
/* 列表扩容 */
public void extendCapacity() {
public void ExtendCapacity() {
// 新建一个长度为 numsCapacity * extendRatio 的数组,并将原数组拷贝到新数组
Array.Resize(ref nums, numsCapacity * extendRatio);
// 更新列表容量
@ -93,11 +93,11 @@ class MyList {
}
/* 将列表转换为数组 */
public int[] toArray() {
public int[] ToArray() {
// 仅转换有效长度范围内的列表元素
int[] nums = new int[numsSize];
for (int i = 0; i < numsSize; i++) {
nums[i] = get(i);
nums[i] = Get(i);
}
return nums;
}
@ -107,38 +107,38 @@ public class my_list {
[Test]
public void Test() {
/* 初始化列表 */
MyList list = new MyList();
MyList list = new();
/* 尾部添加元素 */
list.add(1);
list.add(3);
list.add(2);
list.add(5);
list.add(4);
Console.WriteLine("列表 list = " + string.Join(",", list.toArray()) +
" ,容量 = " + list.capacity() + " ,长度 = " + list.size());
list.Add(1);
list.Add(3);
list.Add(2);
list.Add(5);
list.Add(4);
Console.WriteLine("列表 list = " + string.Join(",", list.ToArray()) +
" ,容量 = " + list.Capacity() + " ,长度 = " + list.Size());
/* 中间插入元素 */
list.insert(3, 6);
Console.WriteLine("在索引 3 处插入数字 6 ,得到 list = " + string.Join(",", list.toArray()));
list.Insert(3, 6);
Console.WriteLine("在索引 3 处插入数字 6 ,得到 list = " + string.Join(",", list.ToArray()));
/* 删除元素 */
list.remove(3);
Console.WriteLine("删除索引 3 处的元素,得到 list = " + string.Join(",", list.toArray()));
list.Remove(3);
Console.WriteLine("删除索引 3 处的元素,得到 list = " + string.Join(",", list.ToArray()));
/* 访问元素 */
int num = list.get(1);
int num = list.Get(1);
Console.WriteLine("访问索引 1 处的元素,得到 num = " + num);
/* 更新元素 */
list.set(1, 0);
Console.WriteLine("将索引 1 处的元素更新为 0 ,得到 list = " + string.Join(",", list.toArray()));
list.Set(1, 0);
Console.WriteLine("将索引 1 处的元素更新为 0 ,得到 list = " + string.Join(",", list.ToArray()));
/* 测试扩容机制 */
for (int i = 0; i < 10; i++) {
// 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制
list.add(i);
list.Add(i);
}
Console.WriteLine("扩容后的列表 list = " + string.Join(",", list.toArray()) +
" ,容量 = " + list.capacity() + " ,长度 = " + list.size());
Console.WriteLine("扩容后的列表 list = " + string.Join(",", list.ToArray()) +
" ,容量 = " + list.Capacity() + " ,长度 = " + list.Size());
}
}