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

@ -8,7 +8,7 @@ namespace hello_algo.chapter_searching;
public class binary_search {
/* 二分查找(双闭区间) */
static int binarySearch(int[] nums, int target) {
static 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) {
static int BinarySearchLCRO(int[] nums, int target) {
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
int i = 0, j = nums.Length;
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
@ -49,11 +49,11 @@ public class binary_search {
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 };
/* 二分查找(双闭区间) */
int index = binarySearch(nums, target);
int index = BinarySearch(nums, target);
Console.WriteLine("目标元素 6 的索引 = " + index);
/* 二分查找(左闭右开) */
index = binarySearchLCRO(nums, target);
index = BinarySearchLCRO(nums, target);
Console.WriteLine("目标元素 6 的索引 = " + index);
}
}

View File

@ -8,9 +8,9 @@ namespace hello_algo.chapter_searching;
public class binary_search_edge {
/* 二分查找最左一个 target */
public int binarySearchLeftEdge(int[] nums, int target) {
public int BinarySearchLeftEdge(int[] nums, int target) {
// 等价于查找 target 的插入点
int i = binary_search_insertion.binarySearchInsertion(nums, target);
int i = binary_search_insertion.BinarySearchInsertion(nums, target);
// 未找到 target ,返回 -1
if (i == nums.Length || nums[i] != target) {
return -1;
@ -22,7 +22,7 @@ public class binary_search_edge {
/* 二分查找最右一个 target */
public int binarySearchRightEdge(int[] nums, int target) {
// 转化为查找最左一个 target + 1
int i = binary_search_insertion.binarySearchInsertion(nums, target + 1);
int i = binary_search_insertion.BinarySearchInsertion(nums, target + 1);
// j 指向最右一个 target i 指向首个大于 target 的元素
int j = i - 1;
// 未找到 target ,返回 -1
@ -41,7 +41,7 @@ public class binary_search_edge {
// 二分查找左边界和右边界
foreach (int target in new int[] { 6, 7 }) {
int index = binarySearchLeftEdge(nums, target);
int index = BinarySearchLeftEdge(nums, target);
Console.WriteLine("最左一个元素 " + target + " 的索引为 " + index);
index = binarySearchRightEdge(nums, target);
Console.WriteLine("最右一个元素 " + target + " 的索引为 " + index);

View File

@ -8,7 +8,7 @@ namespace hello_algo.chapter_searching;
public class binary_search_insertion {
/* 二分查找插入点(无重复元素) */
public int binarySearchInsertionSimple(int[] nums, int target) {
public 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
@ -25,7 +25,7 @@ public class binary_search_insertion {
}
/* 二分查找插入点(存在重复元素) */
public static int binarySearchInsertion(int[] nums, int target) {
public static int BinarySearchInsertion(int[] nums, int target) {
int i = 0, j = nums.Length - 1; // 初始化双闭区间 [0, n-1]
while (i <= j) {
int m = i + (j - i) / 2; // 计算中点索引 m
@ -48,7 +48,7 @@ public class binary_search_insertion {
Console.WriteLine("\n数组 nums = " + nums.PrintList());
// 二分查找插入点
foreach (int target in new int[] { 6, 9 }) {
int index = binarySearchInsertionSimple(nums, target);
int index = BinarySearchInsertionSimple(nums, target);
Console.WriteLine("元素 " + target + " 的插入点的索引为 " + index);
}
@ -57,7 +57,7 @@ public class binary_search_insertion {
Console.WriteLine("\n数组 nums = " + nums.PrintList());
// 二分查找插入点
foreach (int target in new int[] { 2, 6, 20 }) {
int index = binarySearchInsertion(nums, target);
int index = BinarySearchInsertion(nums, target);
Console.WriteLine("元素 " + target + " 的插入点的索引为 " + index);
}
}

View File

@ -8,14 +8,14 @@ namespace hello_algo.chapter_searching;
public class hashing_search {
/* 哈希查找(数组) */
static int hashingSearchArray(Dictionary<int, int> map, int target) {
static 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) {
static ListNode? HashingSearchLinkedList(Dictionary<int, ListNode> map, int target) {
// 哈希表的 key: 目标节点值value: 节点对象
// 若哈希表中无此 key ,返回 null
@ -33,7 +33,7 @@ public class hashing_search {
for (int i = 0; i < nums.Length; i++) {
map[nums[i]] = i; // key: 元素value: 索引
}
int index = hashingSearchArray(map, target);
int index = HashingSearchArray(map, target);
Console.WriteLine("目标元素 3 的索引 = " + index);
/* 哈希查找(链表) */
@ -44,7 +44,7 @@ public class hashing_search {
map1[head.val] = head; // key: 节点值value: 节点
head = head.next;
}
ListNode? node = hashingSearchLinkedList(map1, target);
ListNode? node = HashingSearchLinkedList(map1, target);
Console.WriteLine("目标节点值 3 的对应节点对象为 " + node);
}
}

View File

@ -8,7 +8,7 @@ namespace hello_algo.chapter_searching;
public class linear_search {
/* 线性查找(数组) */
static int linearSearchArray(int[] nums, int target) {
static 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) {
static ListNode? LinearSearchLinkedList(ListNode? head, int target) {
// 遍历链表
while (head != null) {
// 找到目标节点,返回之
@ -38,12 +38,12 @@ public class linear_search {
/* 在数组中执行线性查找 */
int[] nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 };
int index = linearSearchArray(nums, target);
int index = LinearSearchArray(nums, target);
Console.WriteLine("目标元素 3 的索引 = " + index);
/* 在链表中执行线性查找 */
ListNode head = ListNode.ArrToLinkedList(nums);
ListNode? node = linearSearchLinkedList(head, target);
ListNode? head = ListNode.ArrToLinkedList(nums);
ListNode? node = LinearSearchLinkedList(head, target);
Console.WriteLine("目标节点值 3 的对应节点对象为 " + node);
}
}

View File

@ -8,7 +8,7 @@ namespace hello_algo.chapter_searching;
public class two_sum {
/* 方法一:暴力枚举 */
public static int[] twoSumBruteForce(int[] nums, int target) {
public static int[] TwoSumBruteForce(int[] nums, int target) {
int size = nums.Length;
// 两层循环,时间复杂度 O(n^2)
for (int i = 0; i < size - 1; i++) {
@ -21,7 +21,7 @@ public class two_sum {
}
/* 方法二:辅助哈希表 */
public static int[] twoSumHashTable(int[] nums, int target) {
public static int[] TwoSumHashTable(int[] nums, int target) {
int size = nums.Length;
// 辅助哈希表,空间复杂度 O(n)
Dictionary<int, int> dic = new();
@ -43,10 +43,10 @@ public class two_sum {
// ====== Driver Code ======
// 方法一
int[] res = twoSumBruteForce(nums, target);
int[] res = TwoSumBruteForce(nums, target);
Console.WriteLine("方法一 res = " + string.Join(",", res));
// 方法二
res = twoSumHashTable(nums, target);
res = TwoSumHashTable(nums, target);
Console.WriteLine("方法二 res = " + string.Join(",", res));
}
}