mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-14 02:10:37 +08:00
build
This commit is contained in:
@@ -202,9 +202,9 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
|
||||
```csharp title="array.cs"
|
||||
/* 随机返回一个数组元素 */
|
||||
int RandomAccess(int[] nums)
|
||||
int randomAccess(int[] nums)
|
||||
{
|
||||
Random random=new();
|
||||
Random random = new();
|
||||
// 在区间 [0, nums.Length) 中随机抽取一个数字
|
||||
int randomIndex = random.Next(nums.Length);
|
||||
// 获取并返回随机元素
|
||||
@@ -355,7 +355,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
|
||||
```csharp title="array.cs"
|
||||
/* 扩展数组长度 */
|
||||
int[] Extend(int[] nums, int enlarge)
|
||||
int[] extend(int[] nums, int enlarge)
|
||||
{
|
||||
// 初始化一个扩展长度后的数组
|
||||
int[] res = new int[nums.Length + enlarge];
|
||||
@@ -548,7 +548,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
|
||||
```csharp title="array.cs"
|
||||
/* 在数组的索引 index 处插入元素 num */
|
||||
void Insert(int[] nums, int num, int index)
|
||||
void insert(int[] nums, int num, int index)
|
||||
{
|
||||
// 把索引 index 以及之后的所有元素向后移动一位
|
||||
for (int i = nums.Length - 1; i > index; i--)
|
||||
@@ -558,8 +558,9 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
// 将 num 赋给 index 处元素
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* 删除索引 index 处元素 */
|
||||
void Remove(int[] nums, int index)
|
||||
void remove(int[] nums, int index)
|
||||
{
|
||||
// 把索引 index 之后的所有元素向前移动一位
|
||||
for (int i = index; i < nums.Length - 1; i++)
|
||||
@@ -725,7 +726,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
|
||||
```csharp title="array.cs"
|
||||
/* 遍历数组 */
|
||||
void Traverse(int[] nums)
|
||||
void traverse(int[] nums)
|
||||
{
|
||||
int count = 0;
|
||||
// 通过索引遍历数组
|
||||
@@ -868,7 +869,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
|
||||
```csharp title="array.cs"
|
||||
/* 在数组中查找指定元素 */
|
||||
int Find(int[] nums, int target)
|
||||
int find(int[] nums, int target)
|
||||
{
|
||||
for (int i = 0; i < nums.Length; i++)
|
||||
{
|
||||
|
||||
@@ -459,22 +459,22 @@ comments: true
|
||||
=== "C#"
|
||||
|
||||
```csharp title="linked_list.cs"
|
||||
// 在链表的结点 n0 之后插入结点 P
|
||||
void Insert(ListNode n0, ListNode P)
|
||||
/* 在链表的结点 n0 之后插入结点 P */
|
||||
void insert(ListNode n0, ListNode P)
|
||||
{
|
||||
ListNode n1 = n0.next;
|
||||
ListNode? n1 = n0.next;
|
||||
n0.next = P;
|
||||
P.next = n1;
|
||||
}
|
||||
|
||||
// 删除链表的结点 n0 之后的首个结点
|
||||
void Remove(ListNode n0)
|
||||
/* 删除链表的结点 n0 之后的首个结点 */
|
||||
void remove(ListNode n0)
|
||||
{
|
||||
if (n0.next == null)
|
||||
return;
|
||||
// n0 -> P -> n1
|
||||
ListNode P = n0.next;
|
||||
ListNode n1 = P.next;
|
||||
ListNode? n1 = P.next;
|
||||
n0.next = n1;
|
||||
}
|
||||
```
|
||||
@@ -620,8 +620,8 @@ comments: true
|
||||
=== "C#"
|
||||
|
||||
```csharp title="linked_list.cs"
|
||||
// 访问链表中索引为 index 的结点
|
||||
ListNode Access(ListNode head, int index)
|
||||
/* 访问链表中索引为 index 的结点 */
|
||||
ListNode? access(ListNode head, int index)
|
||||
{
|
||||
for (int i = 0; i < index; i++)
|
||||
{
|
||||
@@ -776,8 +776,8 @@ comments: true
|
||||
=== "C#"
|
||||
|
||||
```csharp title="linked_list.cs"
|
||||
// 在链表中查找值为 target 的首个结点
|
||||
int Find(ListNode head, int target)
|
||||
/* 在链表中查找值为 target 的首个结点 */
|
||||
int find(ListNode head, int target)
|
||||
{
|
||||
int index = 0;
|
||||
while (head != null)
|
||||
|
||||
@@ -1323,101 +1323,114 @@ comments: true
|
||||
=== "C#"
|
||||
|
||||
```csharp title="my_list.cs"
|
||||
/* 列表类简易实现 */
|
||||
class MyList
|
||||
{
|
||||
private int[] nums; // 数组(存储列表元素)
|
||||
private int capacity = 10; // 列表容量
|
||||
private int size = 0; // 列表长度(即当前元素数量)
|
||||
private int numsCapacity = 10; // 列表容量
|
||||
private int numsSize = 0; // 列表长度(即当前元素数量)
|
||||
private int extendRatio = 2; // 每次列表扩容的倍数
|
||||
|
||||
/* 构造函数 */
|
||||
public MyList()
|
||||
{
|
||||
nums = new int[capacity];
|
||||
nums = new int[numsCapacity];
|
||||
}
|
||||
|
||||
/* 获取列表长度(即当前元素数量)*/
|
||||
public int Size()
|
||||
public int size()
|
||||
{
|
||||
return size;
|
||||
return numsSize;
|
||||
}
|
||||
|
||||
/* 获取列表容量 */
|
||||
public int Capacity()
|
||||
public int capacity()
|
||||
{
|
||||
return capacity;
|
||||
return numsCapacity;
|
||||
}
|
||||
|
||||
/* 访问元素 */
|
||||
public int Get(int index)
|
||||
public int get(int index)
|
||||
{
|
||||
// 索引如果越界则抛出异常,下同
|
||||
if (index < 0 || index >= size)
|
||||
if (index < 0 || index >= numsSize)
|
||||
throw new IndexOutOfRangeException("索引越界");
|
||||
return nums[index];
|
||||
}
|
||||
|
||||
/* 更新元素 */
|
||||
public void Set(int index, int num)
|
||||
public void set(int index, int num)
|
||||
{
|
||||
if (index < 0 || index >= size)
|
||||
if (index < 0 || index >= numsSize)
|
||||
throw new IndexOutOfRangeException("索引越界");
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* 尾部添加元素 */
|
||||
public void Add(int num)
|
||||
public void add(int num)
|
||||
{
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if (size == Capacity())
|
||||
ExtendCapacity();
|
||||
nums[size] = num;
|
||||
if (numsSize == numsCapacity)
|
||||
extendCapacity();
|
||||
nums[numsSize] = num;
|
||||
// 更新元素数量
|
||||
size++;
|
||||
numsSize++;
|
||||
}
|
||||
|
||||
/* 中间插入元素 */
|
||||
public void Insert(int index, int num)
|
||||
public void insert(int index, int num)
|
||||
{
|
||||
if (index < 0 || index >= size)
|
||||
if (index < 0 || index >= numsSize)
|
||||
throw new IndexOutOfRangeException("索引越界");
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if (size == Capacity())
|
||||
ExtendCapacity();
|
||||
if (numsSize == numsCapacity)
|
||||
extendCapacity();
|
||||
// 将索引 index 以及之后的元素都向后移动一位
|
||||
for (int j = size - 1; j >= index; j--)
|
||||
for (int j = numsSize - 1; j >= index; j--)
|
||||
{
|
||||
nums[j + 1] = nums[j];
|
||||
}
|
||||
nums[index] = num;
|
||||
// 更新元素数量
|
||||
size++;
|
||||
numsSize++;
|
||||
}
|
||||
|
||||
/* 删除元素 */
|
||||
public int Remove(int index)
|
||||
public int remove(int index)
|
||||
{
|
||||
if (index < 0 || index >= size)
|
||||
if (index < 0 || index >= numsSize)
|
||||
throw new IndexOutOfRangeException("索引越界");
|
||||
int num = nums[index];
|
||||
// 将索引 index 之后的元素都向前移动一位
|
||||
for (int j = index; j < size - 1; j++)
|
||||
for (int j = index; j < numsSize - 1; j++)
|
||||
{
|
||||
nums[j] = nums[j + 1];
|
||||
}
|
||||
// 更新元素数量
|
||||
size--;
|
||||
numsSize--;
|
||||
// 返回被删除元素
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 列表扩容 */
|
||||
public void ExtendCapacity()
|
||||
public void extendCapacity()
|
||||
{
|
||||
// 新建一个长度为 size 的数组,并将原数组拷贝到新数组
|
||||
System.Array.Resize(ref nums, Capacity() * extendRatio);
|
||||
// 新建一个长度为 numsCapacity * extendRatio 的数组,并将原数组拷贝到新数组
|
||||
System.Array.Resize(ref nums, numsCapacity * extendRatio);
|
||||
// 更新列表容量
|
||||
capacity = nums.Length;
|
||||
numsCapacity = nums.Length;
|
||||
}
|
||||
|
||||
/* 将列表转换为数组 */
|
||||
public int[] toArray()
|
||||
{
|
||||
// 仅转换有效长度范围内的列表元素
|
||||
int[] nums = new int[numsSize];
|
||||
for (int i = 0; i < numsSize; i++)
|
||||
{
|
||||
nums[i] = get(i);
|
||||
}
|
||||
return nums;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user