mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-12 07:26:07 +00:00
Add build scripts for C# and
unify the coding style.
This commit is contained in:
@@ -166,16 +166,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
=== "C#"
|
||||
|
||||
```csharp title="array.cs"
|
||||
/* 随机返回一个数组元素 */
|
||||
int RandomAccess(int[] nums)
|
||||
{
|
||||
Random random=new();
|
||||
// 在区间 [0, nums.Length) 中随机抽取一个数字
|
||||
int randomIndex = random.Next(nums.Length);
|
||||
// 获取并返回随机元素
|
||||
int randomNum = nums[randomIndex];
|
||||
return randomNum;
|
||||
}
|
||||
[class]{array}-[func]{randomAccess}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -256,19 +247,7 @@ 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;
|
||||
}
|
||||
[class]{array}-[func]{extend}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -373,26 +352,9 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
=== "C#"
|
||||
|
||||
```csharp title="array.cs"
|
||||
/* 在数组的索引 index 处插入元素 num */
|
||||
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 处元素 */
|
||||
void Remove(int[] nums, int index)
|
||||
{
|
||||
// 把索引 index 之后的所有元素向前移动一位
|
||||
for (int i = index; i < nums.Length - 1; i++)
|
||||
{
|
||||
nums[i] = nums[i + 1];
|
||||
}
|
||||
}
|
||||
[class]{array}-[func]{insert}
|
||||
|
||||
[class]{array}-[func]{remove}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -487,21 +449,7 @@ 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++;
|
||||
}
|
||||
}
|
||||
[class]{array}-[func]{traverse}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -586,16 +534,7 @@ 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;
|
||||
}
|
||||
[class]{array}-[func]{find}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
@@ -435,24 +435,9 @@ comments: true
|
||||
=== "C#"
|
||||
|
||||
```csharp title="linked_list.cs"
|
||||
// 在链表的结点 n0 之后插入结点 P
|
||||
void Insert(ListNode n0, ListNode P)
|
||||
{
|
||||
ListNode n1 = n0.next;
|
||||
n0.next = P;
|
||||
P.next = n1;
|
||||
}
|
||||
[class]{linked_list}-[func]{insert}
|
||||
|
||||
// 删除链表的结点 n0 之后的首个结点
|
||||
void Remove(ListNode n0)
|
||||
{
|
||||
if (n0.next == null)
|
||||
return;
|
||||
// n0 -> P -> n1
|
||||
ListNode P = n0.next;
|
||||
ListNode n1 = P.next;
|
||||
n0.next = n1;
|
||||
}
|
||||
[class]{linked_list}-[func]{remove}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -556,17 +541,7 @@ comments: true
|
||||
=== "C#"
|
||||
|
||||
```csharp title="linked_list.cs"
|
||||
// 访问链表中索引为 index 的结点
|
||||
ListNode Access(ListNode head, int index)
|
||||
{
|
||||
for (int i = 0; i < index; i++)
|
||||
{
|
||||
if (head == null)
|
||||
return null;
|
||||
head = head.next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
[class]{linked_list}-[func]{access}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
@@ -652,19 +627,7 @@ comments: true
|
||||
=== "C#"
|
||||
|
||||
```csharp title="linked_list.cs"
|
||||
// 在链表中查找值为 target 的首个结点
|
||||
int Find(ListNode head, int target)
|
||||
{
|
||||
int index = 0;
|
||||
while (head != null)
|
||||
{
|
||||
if (head.val == target)
|
||||
return index;
|
||||
head = head.next;
|
||||
index++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
[class]{linked_list}-[func]{find}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
@@ -854,103 +854,7 @@ comments: true
|
||||
=== "C#"
|
||||
|
||||
```csharp title="my_list.cs"
|
||||
class MyList
|
||||
{
|
||||
private int[] nums; // 数组(存储列表元素)
|
||||
private int capacity = 10; // 列表容量
|
||||
private int size = 0; // 列表长度(即当前元素数量)
|
||||
private int extendRatio = 2; // 每次列表扩容的倍数
|
||||
|
||||
/* 构造函数 */
|
||||
public MyList()
|
||||
{
|
||||
nums = new int[capacity];
|
||||
}
|
||||
|
||||
/* 获取列表长度(即当前元素数量)*/
|
||||
public int Size()
|
||||
{
|
||||
return size;
|
||||
}
|
||||
|
||||
/* 获取列表容量 */
|
||||
public int Capacity()
|
||||
{
|
||||
return capacity;
|
||||
}
|
||||
|
||||
/* 访问元素 */
|
||||
public int Get(int index)
|
||||
{
|
||||
// 索引如果越界则抛出异常,下同
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfRangeException("索引越界");
|
||||
return nums[index];
|
||||
}
|
||||
|
||||
/* 更新元素 */
|
||||
public void Set(int index, int num)
|
||||
{
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfRangeException("索引越界");
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* 尾部添加元素 */
|
||||
public void Add(int num)
|
||||
{
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if (size == Capacity())
|
||||
ExtendCapacity();
|
||||
nums[size] = num;
|
||||
// 更新元素数量
|
||||
size++;
|
||||
}
|
||||
|
||||
/* 中间插入元素 */
|
||||
public void Insert(int index, int num)
|
||||
{
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfRangeException("索引越界");
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if (size == Capacity())
|
||||
ExtendCapacity();
|
||||
// 将索引 index 以及之后的元素都向后移动一位
|
||||
for (int j = size - 1; j >= index; j--)
|
||||
{
|
||||
nums[j + 1] = nums[j];
|
||||
}
|
||||
nums[index] = num;
|
||||
// 更新元素数量
|
||||
size++;
|
||||
}
|
||||
|
||||
/* 删除元素 */
|
||||
public int Remove(int index)
|
||||
{
|
||||
if (index < 0 || index >= size)
|
||||
throw new IndexOutOfRangeException("索引越界");
|
||||
int num = nums[index];
|
||||
// 将索引 index 之后的元素都向前移动一位
|
||||
for (int j = index; j < size - 1; j++)
|
||||
{
|
||||
nums[j] = nums[j + 1];
|
||||
}
|
||||
// 更新元素数量
|
||||
size--;
|
||||
// 返回被删除元素
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 列表扩容 */
|
||||
public void ExtendCapacity()
|
||||
{
|
||||
// 新建一个长度为 size 的数组,并将原数组拷贝到新数组
|
||||
System.Array.Resize(ref nums, Capacity() * extendRatio);
|
||||
// 更新列表容量
|
||||
capacity = nums.Length;
|
||||
}
|
||||
}
|
||||
[class]{MyList}-[func]{}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
Reference in New Issue
Block a user