Add build scripts for C# and

unify the coding style.
This commit is contained in:
krahets
2023-02-08 22:18:02 +08:00
parent 38751cc5f5
commit 6dc21691ed
63 changed files with 2703 additions and 3911 deletions
+2 -38
View File
@@ -113,25 +113,7 @@ $$
=== "C#"
```csharp title="binary_search.cs"
/* 二分查找(双闭区间) */
int binarySearch(int[] nums, int target)
{
// 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素
int i = 0, j = nums.Length - 1;
// 循环,当搜索区间为空时跳出(当 i > j 时为空)
while (i <= j)
{
int m = (i + j) / 2; // 计算中点索引 m
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j] 中
i = m + 1;
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m-1] 中
j = m - 1;
else // 找到目标元素,返回其索引
return m;
}
// 未找到目标元素,返回 -1
return -1;
}
[class]{binary_search}-[func]{binarySearch}
```
=== "Swift"
@@ -212,25 +194,7 @@ $$
=== "C#"
```csharp title="binary_search.cs"
/* 二分查找(左闭右开) */
int binarySearch1(int[] nums, int target)
{
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
int i = 0, j = nums.Length;
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
while (i < j)
{
int m = (i + j) / 2; // 计算中点索引 m
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j) 中
i = m + 1;
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m) 中
j = m;
else // 找到目标元素,返回其索引
return m;
}
// 未找到目标元素,返回 -1
return -1;
}
[class]{binary_search}-[func]{binarySearch1}
```
=== "Swift"
+2 -15
View File
@@ -70,13 +70,7 @@ comments: true
=== "C#"
```csharp title="hashing_search.cs"
/* 哈希查找(数组) */
int hashingSearchArray(Dictionary<int, int> map, int target)
{
// 哈希表的 key: 目标元素,value: 索引
// 若哈希表中无此 key ,返回 -1
return map.GetValueOrDefault(target, -1);
}
[class]{hashing_search}-[func]{hashingSearchArray}
```
=== "Swift"
@@ -149,14 +143,7 @@ comments: true
=== "C#"
```csharp title="hashing_search.cs"
/* 哈希查找(链表) */
ListNode? hashingSearchLinkedList(Dictionary<int, ListNode> map, int target)
{
// 哈希表的 key: 目标结点值,value: 结点对象
// 若哈希表中无此 key ,返回 null
return map.GetValueOrDefault(target);
}
[class]{hashing_search}-[func]{hashingSearchLinkedList}
```
=== "Swift"
+2 -28
View File
@@ -68,20 +68,7 @@ comments: true
=== "C#"
```csharp title="linear_search.cs"
/* 线性查找(数组) */
int linearSearchArray(int[] nums, int target)
{
// 遍历数组
for (int i = 0; i < nums.Length; i++)
{
// 找到目标元素,返回其索引
if (nums[i] == target)
return i;
}
// 未找到目标元素,返回 -1
return -1;
}
[class]{linear_search}-[func]{linearSearchArray}
```
=== "Swift"
@@ -155,20 +142,7 @@ comments: true
=== "C#"
```csharp title="linear_search.cs"
/* 线性查找(链表) */
ListNode? linearSearchLinkedList(ListNode head, int target)
{
// 遍历链表
while (head != null)
{
// 找到目标结点,返回之
if (head.val == target)
return head;
head = head.next;
}
// 未找到目标结点,返回 null
return null;
}
[class]{linear_search}-[func]{linearSearchLinkedList}
```
=== "Swift"