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

Update the book based on the revised second edition #1014

Merged
merged 3 commits into from
Dec 28, 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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -23,10 +23,10 @@ void backtrack(int row, int n, char state[MAX_SIZE][MAX_SIZE], char ***res, int
}
// 遍历所有列
for (int col = 0; col < n; col++) {
// 计算该格子对应的主对角线和副对角线
// 计算该格子对应的主对角线和次对角线
int diag1 = row - col + n - 1;
int diag2 = row + col;
// 剪枝:不允许该格子所在列、主对角线、副对角线上存在皇后
// 剪枝:不允许该格子所在列、主对角线、次对角线上存在皇后
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
// 尝试:将皇后放置在该格子
state[row][col] = 'Q';
Expand All @@ -52,7 +52,7 @@ char ***nQueens(int n, int *returnSize) {
}
bool cols[MAX_SIZE] = {false}; // 记录列是否有皇后
bool diags1[2 * MAX_SIZE - 1] = {false}; // 记录主对角线上是否有皇后
bool diags2[2 * MAX_SIZE - 1] = {false}; // 记录副对角线上是否有皇后
bool diags2[2 * MAX_SIZE - 1] = {false}; // 记录次对角线上是否有皇后

char ***res = (char ***)malloc(sizeof(char **) * MAX_SIZE);
*returnSize = 0;
Expand Down
4 changes: 2 additions & 2 deletions codes/c/chapter_graph/graph_bfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ int isVisited(Vertex **visited, int size, Vertex *vet) {
return 0;
}

/* 广度优先遍历 BFS */
/* 广度优先遍历 */
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
void graphBFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize, Vertex **visited, int *visitedSize) {
// 队列用于实现 BFS
Expand Down Expand Up @@ -98,7 +98,7 @@ int main() {
printf("\n初始化后,图为\n");
printGraph(graph);

// 广度优先遍历 BFS
// 广度优先遍历
// 顶点遍历序列
Vertex *res[MAX_SIZE];
int resSize = 0;
Expand Down
6 changes: 3 additions & 3 deletions codes/c/chapter_graph/graph_dfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ int isVisited(Vertex **res, int size, Vertex *vet) {
return 0;
}

/* 深度优先遍历 DFS 辅助函数 */
/* 深度优先遍历辅助函数 */
void dfs(GraphAdjList *graph, Vertex **res, int *resSize, Vertex *vet) {
// 记录访问顶点
res[(*resSize)++] = vet;
Expand All @@ -36,7 +36,7 @@ void dfs(GraphAdjList *graph, Vertex **res, int *resSize, Vertex *vet) {
}
}

/* 深度优先遍历 DFS */
/* 深度优先遍历 */
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
void graphDFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize) {
dfs(graph, res, resSize, startVet);
Expand All @@ -61,7 +61,7 @@ int main() {
printf("\n初始化后,图为\n");
printGraph(graph);

// 深度优先遍历 DFS
// 深度优先遍历
Vertex *res[MAX_SIZE];
int resSize = 0;
graphDFS(graph, v[0], res, &resSize);
Expand Down
2 changes: 1 addition & 1 deletion codes/c/chapter_greedy/max_capacity.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ int myMax(int a, int b) {

/* 最大容量:贪心 */
int maxCapacity(int ht[], int htLength) {
// 初始化 i, j 分列数组两端
// 初始化 i, j,使其分列数组两端
int i = 0;
int j = htLength - 1;
// 初始最大容量为 0
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 @@ -173,7 +173,7 @@ int main() {
print(hmap);

/* 查询操作 */
// 向哈希表输入键 key ,得到值 value
// 向哈希表中输入键 key ,得到值 value
const char *name = get(hmap, 15937);
printf("\n输入学号 15937 ,查询到姓名 %s\n", name);

Expand Down
6 changes: 3 additions & 3 deletions codes/c/chapter_hashing/hash_map_chaining.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ double loadFactor(HashMapChaining *hashMap) {
/* 查询操作 */
char *get(HashMapChaining *hashMap, int key) {
int index = hashFunc(hashMap, key);
// 遍历桶,若找到 key 则返回对应 val
// 遍历桶,若找到 key 则返回对应 val
Node *cur = hashMap->buckets[index];
while (cur) {
if (cur->pair->key == key) {
return cur->pair->val;
}
cur = cur->next;
}
return ""; // 若未找到 key 则返回空字符串
return ""; // 若未找到 key 则返回空字符串
}

/* 添加操作 */
Expand Down Expand Up @@ -196,7 +196,7 @@ int main() {
print(hashMap);

/* 查询操作 */
// 向哈希表输入键 key ,得到值 value
// 向哈希表中输入键 key ,得到值 value
char *name = get(hashMap, 13276);
printf("\n输入学号 13276 ,查询到姓名 %s\n", name);

Expand Down
8 changes: 4 additions & 4 deletions codes/c/chapter_hashing/hash_map_open_addressing.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ int findBucket(HashMapOpenAddressing *hashMap, int key) {
int firstTombstone = -1;
// 线性探测,当遇到空桶时跳出
while (hashMap->buckets[index] != NULL) {
// 若遇到 key ,返回对应桶索引
// 若遇到 key ,返回对应的桶索引
if (hashMap->buckets[index]->key == key) {
// 若之前遇到了删除标记,则将键值对移动至该索引
// 若之前遇到了删除标记,则将键值对移动至该索引处
if (firstTombstone != -1) {
hashMap->buckets[firstTombstone] = hashMap->buckets[index];
hashMap->buckets[index] = hashMap->TOMBSTONE;
Expand All @@ -81,7 +81,7 @@ int findBucket(HashMapOpenAddressing *hashMap, int key) {
if (firstTombstone == -1 && hashMap->buckets[index] == hashMap->TOMBSTONE) {
firstTombstone = index;
}
// 计算桶索引,越过尾部返回头部
// 计算桶索引,越过尾部则返回头部
index = (index + 1) % hashMap->capacity;
}
// 若 key 不存在,则返回添加点的索引
Expand Down Expand Up @@ -192,7 +192,7 @@ int main() {
print(hashmap);

// 查询操作
// 向哈希表输入键 key ,得到值 val
// 向哈希表中输入键 key ,得到值 val
char *name = get(hashmap, 13276);
printf("\n输入学号 13276 ,查询到姓名 %s\n", name);

Expand Down
6 changes: 3 additions & 3 deletions codes/c/chapter_heap/my_heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ void delMaxHeap(MaxHeap *maxHeap) {
free(maxHeap);
}

/* 获取左子节点索引 */
/* 获取左子节点的索引 */
int left(MaxHeap *maxHeap, int i) {
return 2 * i + 1;
}

/* 获取右子节点索引 */
/* 获取右子节点的索引 */
int right(MaxHeap *maxHeap, int i) {
return 2 * i + 2;
}

/* 获取父节点索引 */
/* 获取父节点的索引 */
int parent(MaxHeap *maxHeap, int i) {
return (i - 1) / 2;
}
Expand Down
4 changes: 2 additions & 2 deletions codes/c/chapter_sorting/merge_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

/* 合并左子数组和右子数组 */
void merge(int *nums, int left, int mid, int right) {
// 左子数组区间 [left, mid], 右子数组区间 [mid+1, right]
// 左子数组区间为 [left, mid], 右子数组区间为 [mid+1, right]
// 创建一个临时数组 tmp ,用于存放合并后的结果
int tmpSize = right - left + 1;
int *tmp = (int *)malloc(tmpSize * sizeof(int));
// 初始化左子数组和右子数组的起始索引
int i = left, j = mid + 1, k = 0;
// 当左右子数组都还有元素时,比较并将较小的元素复制到临时数组中
// 当左右子数组都还有元素时,进行比较并将较小的元素复制到临时数组中
while (i <= mid && j <= right) {
if (nums[i] <= nums[j]) {
tmp[k++] = nums[i++];
Expand Down
2 changes: 1 addition & 1 deletion codes/c/chapter_sorting/quick_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void quickSort(int nums[], int left, int right) {
}

/* 快速排序类(中位基准数优化) */
// 选取三个元素的中位数
// 选取三个候选元素的中位数
int medianThree(int nums[], int left, int mid, int right) {
// 此处使用异或运算来简化代码
// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1
Expand Down
4 changes: 2 additions & 2 deletions codes/c/chapter_stack_and_queue/array_deque.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void pushFirst(ArrayDeque *deque, int num) {
return;
}
// 队首指针向左移动一位
// 通过取余操作,实现 front 越过数组头部回到尾部
// 通过取余操作实现 front 越过数组头部回到尾部
deque->front = dequeIndex(deque, deque->front - 1);
// 将 num 添加到队首
deque->nums[deque->front] = num;
Expand All @@ -73,7 +73,7 @@ void pushLast(ArrayDeque *deque, int num) {
printf("双向队列已满\r\n");
return;
}
// 计算尾指针,指向队尾索引 + 1
// 计算队尾指针,指向队尾索引 + 1
int rear = dequeIndex(deque, deque->front + deque->queSize);
// 将 num 添加至队尾
deque->nums[rear] = num;
Expand Down
4 changes: 2 additions & 2 deletions codes/c/chapter_stack_and_queue/array_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void push(ArrayQueue *queue, int num) {
return;
}
// 计算队尾指针,指向队尾索引 + 1
// 通过取余操作,实现 rear 越过数组尾部后回到头部
// 通过取余操作实现 rear 越过数组尾部后回到头部
int rear = (queue->front + queue->queSize) % queue->queCapacity;
// 将 num 添加至队尾
queue->nums[rear] = num;
Expand All @@ -68,7 +68,7 @@ void push(ArrayQueue *queue, int num) {
/* 出队 */
int pop(ArrayQueue *queue) {
int num = peek(queue);
// 队首指针向后移动一位,若越过尾部则返回到数组头部
// 队首指针向后移动一位,若越过尾部,则返回到数组头部
queue->front = (queue->front + 1) % queue->queCapacity;
queue->queSize--;
return num;
Expand Down
4 changes: 2 additions & 2 deletions codes/c/chapter_tree/avl_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ TreeNode *insertHelper(TreeNode *node, int val) {
if (node == NULL) {
return newTreeNode(val);
}
/* 1. 查找插入位置,并插入节点 */
/* 1. 查找插入位置并插入节点 */
if (val < node->val) {
node->left = insertHelper(node->left, val);
} else if (val > node->val) {
Expand Down Expand Up @@ -148,7 +148,7 @@ TreeNode *removeHelper(TreeNode *node, int val) {
if (node == NULL) {
return NULL;
}
/* 1. 查找节点,并删除之 */
/* 1. 查找节点并删除 */
if (val < node->val) {
node->left = removeHelper(node->left, val);
} else if (val > node->val) {
Expand Down
4 changes: 2 additions & 2 deletions codes/cpp/chapter_array_and_linkedlist/my_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class MyList {

/* 访问元素 */
int get(int index) {
// 索引如果越界则抛出异常,下同
// 索引如果越界,则抛出异常,下同
if (index < 0 || index >= size())
throw out_of_range("索引越界");
return arr[index];
Expand Down Expand Up @@ -87,7 +87,7 @@ class MyList {
}
// 更新元素数量
arrSize--;
// 返回被删除元素
// 返回被删除的元素
return num;
}

Expand Down
6 changes: 3 additions & 3 deletions codes/cpp/chapter_backtracking/n_queens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ void backtrack(int row, int n, vector<vector<string>> &state, vector<vector<vect
}
// 遍历所有列
for (int col = 0; col < n; col++) {
// 计算该格子对应的主对角线和副对角线
// 计算该格子对应的主对角线和次对角线
int diag1 = row - col + n - 1;
int diag2 = row + col;
// 剪枝:不允许该格子所在列、主对角线、副对角线上存在皇后
// 剪枝:不允许该格子所在列、主对角线、次对角线上存在皇后
if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {
// 尝试:将皇后放置在该格子
state[row][col] = "Q";
Expand All @@ -39,7 +39,7 @@ vector<vector<vector<string>>> nQueens(int n) {
vector<vector<string>> state(n, vector<string>(n, "#"));
vector<bool> cols(n, false); // 记录列是否有皇后
vector<bool> diags1(2 * n - 1, false); // 记录主对角线上是否有皇后
vector<bool> diags2(2 * n - 1, false); // 记录副对角线上是否有皇后
vector<bool> diags2(2 * n - 1, false); // 记录次对角线上是否有皇后
vector<vector<vector<string>>> res;

backtrack(0, n, state, res, cols, diags1, diags2);
Expand Down
4 changes: 2 additions & 2 deletions codes/cpp/chapter_graph/graph_bfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "../utils/common.hpp"
#include "./graph_adjacency_list.cpp"

/* 广度优先遍历 BFS */
/* 广度优先遍历 */
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
vector<Vertex *> graphBFS(GraphAdjList &graph, Vertex *startVet) {
// 顶点遍历序列
Expand Down Expand Up @@ -45,7 +45,7 @@ int main() {
cout << "\n初始化后,图为\\n";
graph.print();

/* 广度优先遍历 BFS */
/* 广度优先遍历 */
vector<Vertex *> res = graphBFS(graph, v[0]);
cout << "\n广度优先遍历(BFS)顶点序列为" << endl;
printVector(vetsToVals(res));
Expand Down
6 changes: 3 additions & 3 deletions codes/cpp/chapter_graph/graph_dfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "../utils/common.hpp"
#include "./graph_adjacency_list.cpp"

/* 深度优先遍历 DFS 辅助函数 */
/* 深度优先遍历辅助函数 */
void dfs(GraphAdjList &graph, unordered_set<Vertex *> &visited, vector<Vertex *> &res, Vertex *vet) {
res.push_back(vet); // 记录访问顶点
visited.emplace(vet); // 标记该顶点已被访问
Expand All @@ -20,7 +20,7 @@ void dfs(GraphAdjList &graph, unordered_set<Vertex *> &visited, vector<Vertex *>
}
}

/* 深度优先遍历 DFS */
/* 深度优先遍历 */
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
vector<Vertex *> graphDFS(GraphAdjList &graph, Vertex *startVet) {
// 顶点遍历序列
Expand All @@ -41,7 +41,7 @@ int main() {
cout << "\n初始化后,图为" << endl;
graph.print();

/* 深度优先遍历 DFS */
/* 深度优先遍历 */
vector<Vertex *> res = graphDFS(graph, v[0]);
cout << "\n深度优先遍历(DFS)顶点序列为" << endl;
printVector(vetsToVals(res));
Expand Down
2 changes: 1 addition & 1 deletion codes/cpp/chapter_greedy/max_capacity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

/* 最大容量:贪心 */
int maxCapacity(vector<int> &ht) {
// 初始化 i, j 分列数组两端
// 初始化 i, j,使其分列数组两端
int i = 0, j = ht.size() - 1;
// 初始最大容量为 0
int res = 0;
Expand Down
2 changes: 1 addition & 1 deletion codes/cpp/chapter_hashing/array_hash_map_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ int main() {
map.print();

/* 查询操作 */
// 向哈希表输入键 key ,得到值 value
// 向哈希表中输入键 key ,得到值 value
string name = map.get(15937);
cout << "\n输入学号 15937 ,查询到姓名 " << name << endl;

Expand Down
2 changes: 1 addition & 1 deletion codes/cpp/chapter_hashing/hash_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ int main() {
printHashMap(map);

/* 查询操作 */
// 向哈希表输入键 key ,得到值 value
// 向哈希表中输入键 key ,得到值 value
string name = map[15937];
cout << "\n输入学号 15937 ,查询到姓名 " << name << endl;

Expand Down
6 changes: 3 additions & 3 deletions codes/cpp/chapter_hashing/hash_map_chaining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ class HashMapChaining {
/* 查询操作 */
string get(int key) {
int index = hashFunc(key);
// 遍历桶,若找到 key 则返回对应 val
// 遍历桶,若找到 key 则返回对应 val
for (Pair *pair : buckets[index]) {
if (pair->key == key) {
return pair->val;
}
}
// 若未找到 key 则返回空字符串
// 若未找到 key 则返回空字符串
return "";
}

Expand Down Expand Up @@ -136,7 +136,7 @@ int main() {
map.print();

/* 查询操作 */
// 向哈希表输入键 key ,得到值 value
// 向哈希表中输入键 key ,得到值 value
string name = map.get(13276);
cout << "\n输入学号 13276 ,查询到姓名 " << name << endl;

Expand Down
Loading
Loading