mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
Update the book based on the revised second edition (#1014)
* Revised the book * Update the book with the second revised edition * Revise base on the manuscript of the first edition
This commit is contained in:
@ -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';
|
||||
@ -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;
|
||||
|
||||
@ -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
|
||||
@ -98,7 +98,7 @@ int main() {
|
||||
printf("\n初始化后,图为\n");
|
||||
printGraph(graph);
|
||||
|
||||
// 广度优先遍历 BFS
|
||||
// 广度优先遍历
|
||||
// 顶点遍历序列
|
||||
Vertex *res[MAX_SIZE];
|
||||
int resSize = 0;
|
||||
|
||||
@ -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;
|
||||
@ -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);
|
||||
@ -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);
|
||||
|
||||
@ -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
|
||||
|
||||
@ -173,7 +173,7 @@ int main() {
|
||||
print(hmap);
|
||||
|
||||
/* 查询操作 */
|
||||
// 向哈希表输入键 key ,得到值 value
|
||||
// 向哈希表中输入键 key ,得到值 value
|
||||
const char *name = get(hmap, 15937);
|
||||
printf("\n输入学号 15937 ,查询到姓名 %s\n", name);
|
||||
|
||||
|
||||
@ -74,7 +74,7 @@ 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) {
|
||||
@ -82,7 +82,7 @@ char *get(HashMapChaining *hashMap, int key) {
|
||||
}
|
||||
cur = cur->next;
|
||||
}
|
||||
return ""; // 若未找到 key 则返回空字符串
|
||||
return ""; // 若未找到 key ,则返回空字符串
|
||||
}
|
||||
|
||||
/* 添加操作 */
|
||||
@ -196,7 +196,7 @@ int main() {
|
||||
print(hashMap);
|
||||
|
||||
/* 查询操作 */
|
||||
// 向哈希表输入键 key ,得到值 value
|
||||
// 向哈希表中输入键 key ,得到值 value
|
||||
char *name = get(hashMap, 13276);
|
||||
printf("\n输入学号 13276 ,查询到姓名 %s\n", name);
|
||||
|
||||
|
||||
@ -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;
|
||||
@ -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 不存在,则返回添加点的索引
|
||||
@ -192,7 +192,7 @@ int main() {
|
||||
print(hashmap);
|
||||
|
||||
// 查询操作
|
||||
// 向哈希表输入键 key ,得到值 val
|
||||
// 向哈希表中输入键 key ,得到值 val
|
||||
char *name = get(hashmap, 13276);
|
||||
printf("\n输入学号 13276 ,查询到姓名 %s\n", name);
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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++];
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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;
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
@ -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;
|
||||
|
||||
@ -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) {
|
||||
@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user