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:
@ -35,11 +35,11 @@ void insert(int *nums, int size, int num, int index) {
|
||||
for (int i = size - 1; i > index; i--) {
|
||||
nums[i] = nums[i - 1];
|
||||
}
|
||||
// 将 num 赋给 index 处元素
|
||||
// 将 num 赋给 index 处的元素
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* 删除索引 index 处元素 */
|
||||
/* 删除索引 index 处的元素 */
|
||||
void remove(int *nums, int size, int index) {
|
||||
// 把索引 index 之后的所有元素向前移动一位
|
||||
for (int i = index; i < size - 1; i++) {
|
||||
|
||||
@ -56,7 +56,7 @@ int main() {
|
||||
ListNode *n2 = new ListNode(2);
|
||||
ListNode *n3 = new ListNode(5);
|
||||
ListNode *n4 = new ListNode(4);
|
||||
// 构建引用指向
|
||||
// 构建节点之间的引用
|
||||
n0->next = n1;
|
||||
n1->next = n2;
|
||||
n2->next = n3;
|
||||
|
||||
@ -27,7 +27,7 @@ int main() {
|
||||
cout << "清空列表后 nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* 尾部添加元素 */
|
||||
/* 在尾部添加元素 */
|
||||
nums.push_back(1);
|
||||
nums.push_back(3);
|
||||
nums.push_back(2);
|
||||
@ -36,7 +36,7 @@ int main() {
|
||||
cout << "添加元素后 nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* 中间插入元素 */
|
||||
/* 在中间插入元素 */
|
||||
nums.insert(nums.begin() + 3, 6);
|
||||
cout << "在索引 3 处插入数字 6 ,得到 nums = ";
|
||||
printVector(nums);
|
||||
|
||||
@ -6,12 +6,12 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 列表类简易实现 */
|
||||
/* 列表类 */
|
||||
class MyList {
|
||||
private:
|
||||
int *arr; // 数组(存储列表元素)
|
||||
int arrCapacity = 10; // 列表容量
|
||||
int arrSize = 0; // 列表长度(即当前元素数量)
|
||||
int arrSize = 0; // 列表长度(当前元素数量)
|
||||
int extendRatio = 2; // 每次列表扩容的倍数
|
||||
|
||||
public:
|
||||
@ -25,7 +25,7 @@ class MyList {
|
||||
delete[] arr;
|
||||
}
|
||||
|
||||
/* 获取列表长度(即当前元素数量)*/
|
||||
/* 获取列表长度(当前元素数量)*/
|
||||
int size() {
|
||||
return arrSize;
|
||||
}
|
||||
@ -50,7 +50,7 @@ class MyList {
|
||||
arr[index] = num;
|
||||
}
|
||||
|
||||
/* 尾部添加元素 */
|
||||
/* 在尾部添加元素 */
|
||||
void add(int num) {
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if (size() == capacity())
|
||||
@ -60,7 +60,7 @@ class MyList {
|
||||
arrSize++;
|
||||
}
|
||||
|
||||
/* 中间插入元素 */
|
||||
/* 在中间插入元素 */
|
||||
void insert(int index, int num) {
|
||||
if (index < 0 || index >= size())
|
||||
throw out_of_range("索引越界");
|
||||
@ -121,7 +121,7 @@ class MyList {
|
||||
int main() {
|
||||
/* 初始化列表 */
|
||||
MyList *nums = new MyList();
|
||||
/* 尾部添加元素 */
|
||||
/* 在尾部添加元素 */
|
||||
nums->add(1);
|
||||
nums->add(3);
|
||||
nums->add(2);
|
||||
@ -132,7 +132,7 @@ int main() {
|
||||
printVector(vec);
|
||||
cout << "容量 = " << nums->capacity() << " ,长度 = " << nums->size() << endl;
|
||||
|
||||
/* 中间插入元素 */
|
||||
/* 在中间插入元素 */
|
||||
nums->insert(3, 6);
|
||||
cout << "在索引 3 处插入数字 6 ,得到 nums = ";
|
||||
vec = nums->toVector();
|
||||
|
||||
Reference in New Issue
Block a user