Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Revised the book #978

Merged
merged 20 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 2 additions & 2 deletions codes/c/chapter_array_and_linkedlist/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,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 处的元素 */
// 注意:stdio.h 占用了 remove 关键词
void removeItem(int *nums, int size, int index) {
// 把索引 index 之后的所有元素向前移动一位
Expand Down
2 changes: 1 addition & 1 deletion codes/c/chapter_array_and_linkedlist/linked_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ int main() {
ListNode *n2 = newListNode(2);
ListNode *n3 = newListNode(5);
ListNode *n4 = newListNode(4);
// 构建引用指向
// 构建节点之间的引用
n0->next = n1;
n1->next = n2;
n2->next = n3;
Expand Down
10 changes: 5 additions & 5 deletions codes/c/chapter_array_and_linkedlist/my_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include "../utils/common.h"

/* 列表类简易实现 */
/* 列表类 */
typedef struct {
int *arr; // 数组(存储列表元素)
int capacity; // 列表容量
Expand Down Expand Up @@ -54,7 +54,7 @@ void set(MyList *nums, int index, int num) {
nums->arr[index] = num;
}

/* 尾部添加元素 */
/* 在尾部添加元素 */
void add(MyList *nums, int num) {
if (size(nums) == capacity(nums)) {
extendCapacity(nums); // 扩容
Expand All @@ -63,7 +63,7 @@ void add(MyList *nums, int num) {
nums->size++;
}

/* 中间插入元素 */
/* 在中间插入元素 */
void insert(MyList *nums, int index, int num) {
assert(index >= 0 && index < size(nums));
// 元素数量超出容量时,触发扩容机制
Expand Down Expand Up @@ -117,7 +117,7 @@ int *toArray(MyList *nums) {
int main() {
/* 初始化列表 */
MyList *nums = newMyList();
/* 尾部添加元素 */
/* 在尾部添加元素 */
add(nums, 1);
add(nums, 3);
add(nums, 2);
Expand All @@ -127,7 +127,7 @@ int main() {
printArray(toArray(nums), size(nums));
printf("容量 = %d ,长度 = %d\n", capacity(nums), size(nums));

/* 中间插入元素 */
/* 在中间插入元素 */
insert(nums, 3, 6);
printf("在索引 3 处插入数字 6 ,得到 nums = ");
printArray(toArray(nums), size(nums));
Expand Down
6 changes: 3 additions & 3 deletions codes/c/chapter_backtracking/n_queens.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void backtrack(int row, int n, char state[MAX_SIZE][MAX_SIZE], char ***res, int
// 计算该格子对应的主对角线和副对角线
int diag1 = row - col + n - 1;
int diag2 = row + col;
// 剪枝:不允许该格子所在列、主对角线、副对角线存在皇后
// 剪枝:不允许该格子所在列、主对角线、副对角线上存在皇后
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
// 尝试:将皇后放置在该格子
state[row][col] = 'Q';
Expand All @@ -51,8 +51,8 @@ char ***nQueens(int n, int *returnSize) {
state[i][n] = '\0';
}
bool cols[MAX_SIZE] = {false}; // 记录列是否有皇后
bool diags1[2 * MAX_SIZE - 1] = {false}; // 记录主对角线是否有皇后
bool diags2[2 * MAX_SIZE - 1] = {false}; // 记录副对角线是否有皇后
bool diags1[2 * MAX_SIZE - 1] = {false}; // 记录主对角线上是否有皇后
bool diags2[2 * MAX_SIZE - 1] = {false}; // 记录副对角线上是否有皇后

char ***res = (char ***)malloc(sizeof(char **) * MAX_SIZE);
*returnSize = 0;
Expand Down
2 changes: 1 addition & 1 deletion codes/c/chapter_computational_complexity/iteration.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ int whileLoop(int n) {
int whileLoopII(int n) {
int res = 0;
int i = 1; // 初始化条件变量
// 循环求和 1, 4, ...
// 循环求和 1, 4, 10, ...
while (i <= n) {
res += i;
// 更新条件变量
Expand Down
4 changes: 2 additions & 2 deletions codes/c/chapter_divide_and_conquer/hanota.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void move(int *src, int *srcSize, int *tar, int *tarSize) {
(*tarSize)++;
}

/* 求解汉诺塔:问题 f(i) */
/* 求解汉诺塔问题 f(i) */
void dfs(int i, int *src, int *srcSize, int *buf, int *bufSize, int *tar, int *tarSize) {
// 若 src 只剩下一个圆盘,则直接将其移到 tar
if (i == 1) {
Expand All @@ -35,7 +35,7 @@ void dfs(int i, int *src, int *srcSize, int *buf, int *bufSize, int *tar, int *t
dfs(i - 1, buf, bufSize, src, srcSize, tar, tarSize);
}

/* 求解汉诺塔 */
/* 求解汉诺塔问题 */
void solveHanota(int *A, int *ASize, int *B, int *BSize, int *C, int *CSize) {
// 将 A 顶部 n 个圆盘借助 B 移到 C
dfs(*ASize, A, ASize, B, BSize, C, CSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void backtrack(int *choices, int state, int n, int *res, int len) {

/* 爬楼梯:回溯 */
int climbingStairsBacktrack(int n) {
int choices[2] = {1, 2}; // 可选择向上爬 1 2 阶
int choices[2] = {1, 2}; // 可选择向上爬 1 阶或 2 阶
int state = 0; // 从第 0 阶开始爬
int *res = (int *)malloc(sizeof(int));
*res = 0; // 使用 res[0] 记录方案数量
Expand Down
2 changes: 1 addition & 1 deletion codes/c/chapter_dynamic_programming/coin_change.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ int coinChangeDP(int coins[], int amt, int coinsSize) {
for (int a = 1; a <= amt; a++) {
dp[0][a] = MAX;
}
// 状态转移:其余行列
// 状态转移:其余行和列
for (int i = 1; i <= n; i++) {
for (int a = 1; a <= amt; a++) {
if (coins[i - 1] > a) {
Expand Down
2 changes: 1 addition & 1 deletion codes/c/chapter_dynamic_programming/edit_distance.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ int editDistanceDP(char *s, char *t, int n, int m) {
for (int j = 1; j <= m; j++) {
dp[0][j] = j;
}
// 状态转移:其余行列
// 状态转移:其余行和列
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (s[i - 1] == t[j - 1]) {
Expand Down
8 changes: 4 additions & 4 deletions codes/c/chapter_dynamic_programming/knapsack.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ int myMax(int a, int b) {

/* 0-1 背包:暴力搜索 */
int knapsackDFS(int wgt[], int val[], int i, int c) {
// 若已选完所有物品或背包无容量,则返回价值 0
// 若已选完所有物品或背包无剩余容量,则返回价值 0
if (i == 0 || c == 0) {
return 0;
}
// 若超过背包容量,则只能不放入背包
// 若超过背包容量,则只能选择不放入背包
if (wgt[i - 1] > c) {
return knapsackDFS(wgt, val, i - 1, c);
}
Expand All @@ -30,15 +30,15 @@ int knapsackDFS(int wgt[], int val[], int i, int c) {

/* 0-1 背包:记忆化搜索 */
int knapsackDFSMem(int wgt[], int val[], int memCols, int **mem, int i, int c) {
// 若已选完所有物品或背包无容量,则返回价值 0
// 若已选完所有物品或背包无剩余容量,则返回价值 0
if (i == 0 || c == 0) {
return 0;
}
// 若已有记录,则直接返回
if (mem[i][c] != -1) {
return mem[i][c];
}
// 若超过背包容量,则只能不放入背包
// 若超过背包容量,则只能选择不放入背包
if (wgt[i - 1] > c) {
return knapsackDFSMem(wgt, val, memCols, mem, i - 1, c);
}
Expand Down
2 changes: 1 addition & 1 deletion codes/c/chapter_dynamic_programming/min_path_sum.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ int minPathSumDP(int grid[MAX_SIZE][MAX_SIZE], int n, int m) {
for (int i = 1; i < n; i++) {
dp[i][0] = dp[i - 1][0] + grid[i][0];
}
// 状态转移:其余行列
// 状态转移:其余行和列
for (int i = 1; i < n; i++) {
for (int j = 1; j < m; j++) {
dp[i][j] = myMin(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];
Expand Down
2 changes: 1 addition & 1 deletion codes/c/chapter_graph/graph_bfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void graphBFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize,
// 遍历该顶点的所有邻接顶点
AdjListNode *node = findNode(graph, vet);
while (node != NULL) {
// 跳过已被访问过的顶点
// 跳过已被访问的顶点
if (!isVisited(visited, *visitedSize, node->vertex)) {
enqueue(queue, node->vertex); // 只入队未访问的顶点
visited[(*visitedSize)++] = node->vertex; // 标记该顶点已被访问
Expand Down
2 changes: 1 addition & 1 deletion codes/c/chapter_graph/graph_dfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void dfs(GraphAdjList *graph, Vertex **res, int *resSize, Vertex *vet) {
// 遍历该顶点的所有邻接顶点
AdjListNode *node = findNode(graph, vet);
while (node != NULL) {
// 跳过已被访问过的顶点
// 跳过已被访问的顶点
if (!isVisited(res, *resSize, node->vertex)) {
// 递归访问邻接顶点
dfs(graph, res, resSize, node->vertex);
Expand Down
2 changes: 1 addition & 1 deletion codes/c/chapter_hashing/array_hash_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ typedef struct {
int len;
} MapSet;

/* 基于数组简易实现的哈希表 */
/* 基于数组实现的哈希表 */
typedef struct {
Pair *buckets[HASHTABLE_CAPACITY];
} ArrayHashMap;
Expand Down
2 changes: 1 addition & 1 deletion codes/c/chapter_heap/my_heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ int pop(MaxHeap *maxHeap) {
printf("heap is empty!");
return INT_MAX;
}
// 交换根节点与最右叶节点(即交换首元素与尾元素
// 交换根节点与最右叶节点(交换首元素与尾元素
swap(maxHeap, 0, size(maxHeap) - 1);
// 删除节点
int val = maxHeap->data[maxHeap->size - 1];
Expand Down
6 changes: 3 additions & 3 deletions codes/c/chapter_searching/binary_search.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ int binarySearch(int *nums, int len, int target) {
return -1;
}

/* 二分查找(左闭右开) */
/* 二分查找(左闭右开区间) */
int binarySearchLCRO(int *nums, int len, int target) {
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
// 初始化左闭右开区间 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
int i = 0, j = len;
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
while (i < j) {
Expand All @@ -51,7 +51,7 @@ int main() {
int index = binarySearch(nums, 10, target);
printf("目标元素 6 的索引 = %d\n", index);

/* 二分查找(左闭右开) */
/* 二分查找(左闭右开区间) */
index = binarySearchLCRO(nums, 10, target);
printf("目标元素 6 的索引 = %d\n", index);

Expand Down
2 changes: 1 addition & 1 deletion codes/c/chapter_sorting/bucket_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void bucketSort(float nums[], int size) {

// 1. 将数组元素分配到各个桶中
for (int i = 0; i < size; i++) {
// 输入数据范围 [0, 1),使用 num * k 映射到索引范围 [0, k-1]
// 输入数据范围为 [0, 1),使用 num * k 映射到索引范围 [0, k-1]
int bucket_idx = nums[i] * k;
int j = 0;
// 如果桶中有数据且数据小于当前值 nums[i], 要将其放到当前桶的后面,相当于 cpp 中的 push_back
Expand Down
2 changes: 1 addition & 1 deletion codes/c/chapter_sorting/heap_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void heapSort(int nums[], int n) {
}
// 从堆中提取最大元素,循环 n-1 轮
for (int i = n - 1; i > 0; --i) {
// 交换根节点与最右叶节点(即交换首元素与尾元素
// 交换根节点与最右叶节点(交换首元素与尾元素
int tmp = nums[0];
nums[0] = nums[i];
nums[i] = tmp;
Expand Down
6 changes: 3 additions & 3 deletions codes/c/chapter_sorting/quick_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void swap(int nums[], int i, int j) {
/* 快速排序类 */
// 快速排序类-哨兵划分
int partition(int nums[], int left, int right) {
// 以 nums[left] 作为基准数
// 以 nums[left] 为基准数
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left]) {
Expand Down Expand Up @@ -68,7 +68,7 @@ int partitionMedian(int nums[], int left, int right) {
int med = medianThree(nums, left, (left + right) / 2, right);
// 将中位数交换至数组最左端
swap(nums, left, med);
// 以 nums[left] 作为基准数
// 以 nums[left] 为基准数
int i = left, j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left])
Expand Down Expand Up @@ -100,7 +100,7 @@ void quickSortTailCall(int nums[], int left, int right) {
while (left < right) {
// 哨兵划分操作
int pivot = partition(nums, left, right);
// 对两个子数组中较短的那个执行快排
// 对两个子数组中较短的那个执行快速排序
if (pivot - left < right - pivot) {
quickSortTailCall(nums, left, pivot - 1); // 递归排序左子数组
left = pivot + 1; // 剩余未排序区间为 [pivot + 1, right]
Expand Down
2 changes: 1 addition & 1 deletion codes/c/chapter_sorting/radix_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ int digit(int num, int exp) {

/* 计数排序(根据 nums 第 k 位排序) */
void countingSortDigit(int nums[], int size, int exp) {
// 十进制的位范围为 0~9 ,因此需要长度为 10 的桶
// 十进制的位范围为 0~9 ,因此需要长度为 10 的桶数组
int *counter = (int *)malloc((sizeof(int) * 10));
// 统计 0~9 各数字的出现次数
for (int i = 0; i < size; i++) {
Expand Down
2 changes: 1 addition & 1 deletion codes/c/chapter_stack_and_queue/linkedlist_deque.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ bool empty(LinkedListDeque *deque) {
/* 入队 */
void push(LinkedListDeque *deque, int num, bool isFront) {
DoublyListNode *node = newDoublyListNode(num);
// 若链表为空,则令 front, rear 都指向node
// 若链表为空,则令 front rear 都指向node
if (empty(deque)) {
deque->front = deque->rear = node;
}
Expand Down
2 changes: 1 addition & 1 deletion codes/c/chapter_tree/binary_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ int main() {
TreeNode *n3 = newTreeNode(3);
TreeNode *n4 = newTreeNode(4);
TreeNode *n5 = newTreeNode(5);
// 构建引用指向(即指针
// 构建节点之间的引用(指针
n1->left = n2;
n1->right = n3;
n2->left = n4;
Expand Down
4 changes: 2 additions & 2 deletions codes/cpp/chapter_array_and_linkedlist/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
2 changes: 1 addition & 1 deletion codes/cpp/chapter_array_and_linkedlist/linked_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions codes/cpp/chapter_array_and_linkedlist/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ int main() {
cout << "清空列表后 nums = ";
printVector(nums);

/* 尾部添加元素 */
/* 在尾部添加元素 */
nums.push_back(1);
nums.push_back(3);
nums.push_back(2);
Expand All @@ -36,7 +36,7 @@ int main() {
cout << "添加元素后 nums = ";
printVector(nums);

/* 中间插入元素 */
/* 在中间插入元素 */
nums.insert(nums.begin() + 3, 6);
cout << "在索引 3 处插入数字 6 ,得到 nums = ";
printVector(nums);
Expand Down
Loading
Loading