This commit is contained in:
krahets
2023-10-08 01:43:28 +08:00
parent 3d2d669b43
commit baac2d11a7
52 changed files with 999 additions and 625 deletions
+6 -6
View File
@@ -173,7 +173,7 @@ comments: true
```csharp title="array.cs"
/* 随机访问元素 */
int randomAccess(int[] nums) {
int RandomAccess(int[] nums) {
Random random = new();
// 在区间 [0, nums.Length) 中随机抽取一个数字
int randomIndex = random.Next(nums.Length);
@@ -341,7 +341,7 @@ comments: true
```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--) {
nums[i] = nums[i - 1];
@@ -512,7 +512,7 @@ comments: true
```csharp title="array.cs"
/* 删除索引 index 处元素 */
void remove(int[] nums, int index) {
void Remove(int[] nums, int index) {
// 把索引 index 之后的所有元素向前移动一位
for (int i = index; i < nums.Length - 1; i++) {
nums[i] = nums[i + 1];
@@ -680,7 +680,7 @@ comments: true
```csharp title="array.cs"
/* 遍历数组 */
void traverse(int[] nums) {
void Traverse(int[] nums) {
int count = 0;
// 通过索引遍历数组
for (int i = 0; i < nums.Length; i++) {
@@ -879,7 +879,7 @@ comments: true
```csharp title="array.cs"
/* 在数组中查找指定元素 */
int find(int[] nums, int target) {
int Find(int[] nums, int target) {
for (int i = 0; i < nums.Length; i++) {
if (nums[i] == target)
return i;
@@ -1053,7 +1053,7 @@ comments: true
```csharp title="array.cs"
/* 扩展数组长度 */
int[] extend(int[] nums, int enlarge) {
int[] Extend(int[] nums, int enlarge) {
// 初始化一个扩展长度后的数组
int[] res = new int[nums.Length + enlarge];
// 将原数组中的所有元素复制到新数组
@@ -252,11 +252,11 @@ comments: true
```csharp title="linked_list.cs"
/* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */
// 初始化各个节点
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;
@@ -449,7 +449,7 @@ comments: true
```csharp title="linked_list.cs"
/* 在链表的节点 n0 之后插入节点 P */
void insert(ListNode n0, ListNode P) {
void Insert(ListNode n0, ListNode P) {
ListNode? n1 = n0.next;
P.next = n1;
n0.next = P;
@@ -602,7 +602,7 @@ comments: true
```csharp title="linked_list.cs"
/* 删除链表的节点 n0 之后的首个节点 */
void remove(ListNode n0) {
void Remove(ListNode n0) {
if (n0.next == null)
return;
// n0 -> P -> n1
@@ -778,7 +778,7 @@ comments: true
```csharp title="linked_list.cs"
/* 访问链表中索引为 index 的节点 */
ListNode? access(ListNode head, int index) {
ListNode? Access(ListNode head, int index) {
for (int i = 0; i < index; i++) {
if (head == null)
return null;
@@ -957,7 +957,7 @@ comments: true
```csharp title="linked_list.cs"
/* 在链表中查找值为 target 的首个节点 */
int find(ListNode head, int target) {
int Find(ListNode head, int target) {
int index = 0;
while (head != null) {
if (head.val == target)
+14 -14
View File
@@ -51,7 +51,7 @@ comments: true
```csharp title="list.cs"
/* 初始化列表 */
// 无初始值
List<int> list1 = new ();
List<int> list1 = new();
// 有初始值
int[] numbers = new int[] { 1, 3, 2, 5, 4 };
List<int> list = numbers.ToList();
@@ -1170,7 +1170,7 @@ comments: true
private int[] nums; // 数组(存储列表元素)
private int numsCapacity = 10; // 列表容量
private int numsSize = 0; // 列表长度(即当前元素数量)
private int extendRatio = 2; // 每次列表扩容的倍数
private readonly int extendRatio = 2; // 每次列表扩容的倍数
/* 构造方法 */
public MyList() {
@@ -1178,17 +1178,17 @@ comments: true
}
/* 获取列表长度(即当前元素数量)*/
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("索引越界");
@@ -1196,29 +1196,29 @@ comments: true
}
/* 更新元素 */
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];
@@ -1229,7 +1229,7 @@ comments: true
}
/* 删除元素 */
public int remove(int index) {
public int Remove(int index) {
if (index < 0 || index >= numsSize)
throw new IndexOutOfRangeException("索引越界");
int num = nums[index];
@@ -1244,7 +1244,7 @@ comments: true
}
/* 列表扩容 */
public void extendCapacity() {
public void ExtendCapacity() {
// 新建一个长度为 numsCapacity * extendRatio 的数组,并将原数组拷贝到新数组
Array.Resize(ref nums, numsCapacity * extendRatio);
// 更新列表容量
@@ -1252,11 +1252,11 @@ comments: true
}
/* 将列表转换为数组 */
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;
}