mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
feat: Revised the book (#978)
* Sync recent changes to the revised Word. * Revised the preface chapter * Revised the introduction chapter * Revised the computation complexity chapter * Revised the chapter data structure * Revised the chapter array and linked list * Revised the chapter stack and queue * Revised the chapter hashing * Revised the chapter tree * Revised the chapter heap * Revised the chapter graph * Revised the chapter searching * Reivised the sorting chapter * Revised the divide and conquer chapter * Revised the chapter backtacking * Revised the DP chapter * Revised the greedy chapter * Revised the appendix chapter * Revised the preface chapter doubly * Revised the figures
This commit is contained in:
@ -33,11 +33,11 @@ public class array {
|
||||
for (int i = nums.Length - 1; i > index; i--) {
|
||||
nums[i] = nums[i - 1];
|
||||
}
|
||||
// 将 num 赋给 index 处元素
|
||||
// 将 num 赋给 index 处的元素
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* 删除索引 index 处元素 */
|
||||
/* 删除索引 index 处的元素 */
|
||||
void Remove(int[] nums, int index) {
|
||||
// 把索引 index 之后的所有元素向前移动一位
|
||||
for (int i = index; i < nums.Length - 1; i++) {
|
||||
|
||||
@ -54,7 +54,7 @@ public class linked_list {
|
||||
ListNode n2 = new(2);
|
||||
ListNode n3 = new(5);
|
||||
ListNode n4 = new(4);
|
||||
// 构建引用指向
|
||||
// 构建节点之间的引用
|
||||
n0.next = n1;
|
||||
n1.next = n2;
|
||||
n2.next = n3;
|
||||
|
||||
@ -27,7 +27,7 @@ public class list {
|
||||
nums.Clear();
|
||||
Console.WriteLine("清空列表后 nums = " + string.Join(",", nums));
|
||||
|
||||
/* 尾部添加元素 */
|
||||
/* 在尾部添加元素 */
|
||||
nums.Add(1);
|
||||
nums.Add(3);
|
||||
nums.Add(2);
|
||||
@ -35,7 +35,7 @@ public class list {
|
||||
nums.Add(4);
|
||||
Console.WriteLine("添加元素后 nums = " + string.Join(",", nums));
|
||||
|
||||
/* 中间插入元素 */
|
||||
/* 在中间插入元素 */
|
||||
nums.Insert(3, 6);
|
||||
Console.WriteLine("在索引 3 处插入数字 6 ,得到 nums = " + string.Join(",", nums));
|
||||
|
||||
|
||||
@ -6,11 +6,11 @@
|
||||
|
||||
namespace hello_algo.chapter_array_and_linkedlist;
|
||||
|
||||
/* 列表类简易实现 */
|
||||
/* 列表类 */
|
||||
class MyList {
|
||||
private int[] arr; // 数组(存储列表元素)
|
||||
private int arrCapacity = 10; // 列表容量
|
||||
private int arrSize = 0; // 列表长度(即当前元素数量)
|
||||
private int arrSize = 0; // 列表长度(当前元素数量)
|
||||
private readonly int extendRatio = 2; // 每次列表扩容的倍数
|
||||
|
||||
/* 构造方法 */
|
||||
@ -18,7 +18,7 @@ class MyList {
|
||||
arr = new int[arrCapacity];
|
||||
}
|
||||
|
||||
/* 获取列表长度(即当前元素数量)*/
|
||||
/* 获取列表长度(当前元素数量)*/
|
||||
public int Size() {
|
||||
return arrSize;
|
||||
}
|
||||
@ -43,7 +43,7 @@ class MyList {
|
||||
arr[index] = num;
|
||||
}
|
||||
|
||||
/* 尾部添加元素 */
|
||||
/* 在尾部添加元素 */
|
||||
public void Add(int num) {
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if (arrSize == arrCapacity)
|
||||
@ -53,7 +53,7 @@ class MyList {
|
||||
arrSize++;
|
||||
}
|
||||
|
||||
/* 中间插入元素 */
|
||||
/* 在中间插入元素 */
|
||||
public void Insert(int index, int num) {
|
||||
if (index < 0 || index >= arrSize)
|
||||
throw new IndexOutOfRangeException("索引越界");
|
||||
@ -108,7 +108,7 @@ public class my_list {
|
||||
public void Test() {
|
||||
/* 初始化列表 */
|
||||
MyList nums = new();
|
||||
/* 尾部添加元素 */
|
||||
/* 在尾部添加元素 */
|
||||
nums.Add(1);
|
||||
nums.Add(3);
|
||||
nums.Add(2);
|
||||
@ -117,7 +117,7 @@ public class my_list {
|
||||
Console.WriteLine("列表 nums = " + string.Join(",", nums.ToArray()) +
|
||||
" ,容量 = " + nums.Capacity() + " ,长度 = " + nums.Size());
|
||||
|
||||
/* 中间插入元素 */
|
||||
/* 在中间插入元素 */
|
||||
nums.Insert(3, 6);
|
||||
Console.WriteLine("在索引 3 处插入数字 6 ,得到 nums = " + string.Join(",", nums.ToArray()));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user