mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 23:56:29 +08:00
build
This commit is contained in:
parent
a42c1d62b9
commit
368cbf4261
10 changed files with 195 additions and 212 deletions
|
@ -25,7 +25,7 @@ comments: true
|
|||
|
||||
!!! question "为什么数组要求相同类型的元素,而在链表中却没有强调同类型呢?"
|
||||
|
||||
链表由结点组成,结点之间通过引用(指针)连接,各个结点可以存储不同类型的数据,例如 int、double、string、object 等。
|
||||
链表由节点组成,节点之间通过引用(指针)连接,各个节点可以存储不同类型的数据,例如 int、double、string、object 等。
|
||||
|
||||
相对地,数组元素则必须是相同类型的,这样才能通过计算偏移量来获取对应元素位置。例如,如果数组同时包含 int 和 long 两种类型,单个元素分别占用 4 bytes 和 8 bytes ,那么此时就不能用以下公式计算偏移量了,因为数组中包含了两种长度的元素。
|
||||
|
||||
|
@ -35,19 +35,19 @@ comments: true
|
|||
|
||||
!!! question "删除节点后,是否需要把 `P.next` 设为 $\text{None}$ 呢?"
|
||||
|
||||
不修改 `P.next` 也可以。从该链表的角度看,从头结点遍历到尾结点已经遇不到 `P` 了。这意味着结点 `P` 已经从链表中删除了,此时结点 `P` 指向哪里都不会对这条链表产生影响了。
|
||||
不修改 `P.next` 也可以。从该链表的角度看,从头节点遍历到尾节点已经遇不到 `P` 了。这意味着节点 `P` 已经从链表中删除了,此时节点 `P` 指向哪里都不会对这条链表产生影响了。
|
||||
|
||||
从垃圾回收的角度看,对于 Java、Python、Go 等拥有自动垃圾回收的语言来说,节点 `P` 是否被回收取决于是否有仍存在指向它的引用,而不是 `P.next` 的值。在 C 和 C++ 等语言中,我们需要手动释放节点内存。
|
||||
|
||||
!!! question "在链表中插入和删除操作的时间复杂度是 $O(1)$ 。但是增删之前都需要 $O(n)$ 查找元素,那为什么时间复杂度不是 $O(n)$ 呢?"
|
||||
|
||||
如果是先查找元素、再删除元素,确实是 $O(n)$ 。然而,链表的 $O(1)$ 增删的优势可以在其他应用上得到体现。例如,双向队列适合使用链表实现,我们维护一个指针变量始终指向头结点、尾结点,每次插入与删除操作都是 $O(1)$ 。
|
||||
如果是先查找元素、再删除元素,确实是 $O(n)$ 。然而,链表的 $O(1)$ 增删的优势可以在其他应用上得到体现。例如,双向队列适合使用链表实现,我们维护一个指针变量始终指向头节点、尾节点,每次插入与删除操作都是 $O(1)$ 。
|
||||
|
||||
!!! question "图片“链表定义与存储方式”中,浅蓝色的存储结点指针是占用一块内存地址吗?还是和结点值各占一半呢?"
|
||||
!!! question "图片“链表定义与存储方式”中,浅蓝色的存储节点指针是占用一块内存地址吗?还是和节点值各占一半呢?"
|
||||
|
||||
文中的示意图只是定性表示,定量表示需要根据具体情况进行分析。
|
||||
|
||||
- 不同类型的结点值占用的空间是不同的,比如 int、long、double 和实例对象等。
|
||||
- 不同类型的节点值占用的空间是不同的,比如 int、long、double 和实例对象等。
|
||||
- 指针变量占用的内存空间大小根据所使用的操作系统及编译环境而定,大多为 8 字节或 4 字节。
|
||||
|
||||
!!! question "在列表末尾添加元素是否时时刻刻都为 $O(1)$ ?"
|
||||
|
|
|
@ -193,7 +193,7 @@ comments: true
|
|||
}
|
||||
if (root->val == 7) {
|
||||
// 记录解
|
||||
vectorPushback(res, root, sizeof(int));
|
||||
res[resSize++] = root;
|
||||
}
|
||||
preOrder(root->left);
|
||||
preOrder(root->right);
|
||||
|
@ -214,7 +214,7 @@ comments: true
|
|||
|
||||
**之所以称之为回溯算法,是因为该算法在搜索解空间时会采用“尝试”与“回退”的策略**。当算法在搜索过程中遇到某个状态无法继续前进或无法得到满足条件的解时,它会撤销上一步的选择,退回到之前的状态,并尝试其他可能的选择。
|
||||
|
||||
对于例题一,访问每个节点都代表一次“尝试”,而越过叶结点或返回父节点的 `return` 则表示“回退”。
|
||||
对于例题一,访问每个节点都代表一次“尝试”,而越过叶节点或返回父节点的 `return` 则表示“回退”。
|
||||
|
||||
值得说明的是,**回退并不仅仅包括函数返回**。为解释这一点,我们对例题一稍作拓展。
|
||||
|
||||
|
@ -446,26 +446,23 @@ comments: true
|
|||
|
||||
```c title="preorder_traversal_ii_compact.c"
|
||||
/* 前序遍历:例题二 */
|
||||
void preOrder(TreeNode *root, vector *path, vector *res) {
|
||||
void preOrder(TreeNode *root) {
|
||||
if (root == NULL) {
|
||||
return;
|
||||
}
|
||||
// 尝试
|
||||
vectorPushback(path, root, sizeof(TreeNode));
|
||||
path[pathSize++] = root;
|
||||
if (root->val == 7) {
|
||||
// 记录解
|
||||
vector *newPath = newVector();
|
||||
for (int i = 0; i < path->size; i++) {
|
||||
vectorPushback(newPath, path->data[i], sizeof(int));
|
||||
for (int i = 0; i < pathSize; ++i) {
|
||||
res[resSize][i] = path[i];
|
||||
}
|
||||
vectorPushback(res, newPath, sizeof(vector));
|
||||
resSize++;
|
||||
}
|
||||
|
||||
preOrder(root->left, path, res);
|
||||
preOrder(root->right, path, res);
|
||||
|
||||
preOrder(root->left);
|
||||
preOrder(root->right);
|
||||
// 回退
|
||||
vectorPopback(path);
|
||||
pathSize--;
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -755,28 +752,24 @@ comments: true
|
|||
|
||||
```c title="preorder_traversal_iii_compact.c"
|
||||
/* 前序遍历:例题三 */
|
||||
void preOrder(TreeNode *root, vector *path, vector *res) {
|
||||
void preOrder(TreeNode *root) {
|
||||
// 剪枝
|
||||
if (root == NULL || root->val == 3) {
|
||||
return;
|
||||
}
|
||||
// 尝试
|
||||
vectorPushback(path, root, sizeof(TreeNode));
|
||||
path[pathSize++] = root;
|
||||
if (root->val == 7) {
|
||||
// 记录解
|
||||
vector *newPath = newVector();
|
||||
for (int i = 0; i < path->size; i++) {
|
||||
vectorPushback(newPath, path->data[i], sizeof(int));
|
||||
for (int i = 0; i < pathSize; i++) {
|
||||
res[resSize][i] = path[i];
|
||||
}
|
||||
vectorPushback(res, newPath, sizeof(vector));
|
||||
res->depth++;
|
||||
resSize++;
|
||||
}
|
||||
|
||||
preOrder(root->left, path, res);
|
||||
preOrder(root->right, path, res);
|
||||
|
||||
preOrder(root->left);
|
||||
preOrder(root->right);
|
||||
// 回退
|
||||
vectorPopback(path);
|
||||
pathSize--;
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -1595,56 +1588,52 @@ comments: true
|
|||
|
||||
```c title="preorder_traversal_iii_template.c"
|
||||
/* 判断当前状态是否为解 */
|
||||
bool isSolution(vector *state) {
|
||||
return state->size != 0 && ((TreeNode *)(state->data[state->size - 1]))->val == 7;
|
||||
bool isSolution(void) {
|
||||
return pathSize > 0 && path[pathSize - 1]->val == 7;
|
||||
}
|
||||
|
||||
/* 记录解 */
|
||||
void recordSolution(vector *state, vector *res) {
|
||||
vector *newPath = newVector();
|
||||
for (int i = 0; i < state->size; i++) {
|
||||
vectorPushback(newPath, state->data[i], sizeof(int));
|
||||
void recordSolution(void) {
|
||||
for (int i = 0; i < pathSize; i++) {
|
||||
res[resSize][i] = path[i];
|
||||
}
|
||||
vectorPushback(res, newPath, sizeof(vector));
|
||||
resSize++;
|
||||
}
|
||||
|
||||
/* 判断在当前状态下,该选择是否合法 */
|
||||
bool isValid(vector *state, TreeNode *choice) {
|
||||
bool isValid(TreeNode *choice) {
|
||||
return choice != NULL && choice->val != 3;
|
||||
}
|
||||
|
||||
/* 更新状态 */
|
||||
void makeChoice(vector *state, TreeNode *choice) {
|
||||
vectorPushback(state, choice, sizeof(TreeNode));
|
||||
void makeChoice(TreeNode *choice) {
|
||||
path[pathSize++] = choice;
|
||||
}
|
||||
|
||||
/* 恢复状态 */
|
||||
void undoChoice(vector *state, TreeNode *choice) {
|
||||
vectorPopback(state);
|
||||
void undoChoice(void) {
|
||||
pathSize--;
|
||||
}
|
||||
|
||||
/* 回溯算法:例题三 */
|
||||
void backtrack(vector *state, vector *choices, vector *res) {
|
||||
void backtrack(TreeNode *choices[2]) {
|
||||
// 检查是否为解
|
||||
if (isSolution(state)) {
|
||||
if (isSolution()) {
|
||||
// 记录解
|
||||
recordSolution(state, res);
|
||||
return;
|
||||
recordSolution();
|
||||
}
|
||||
// 遍历所有选择
|
||||
for (int i = 0; i < choices->size; i++) {
|
||||
TreeNode *choice = choices->data[i];
|
||||
for (int i = 0; i < 2; i++) {
|
||||
TreeNode *choice = choices[i];
|
||||
// 剪枝:检查选择是否合法
|
||||
if (isValid(state, choice)) {
|
||||
if (isValid(choice)) {
|
||||
// 尝试:做出选择,更新状态
|
||||
makeChoice(state, choice);
|
||||
makeChoice(choice);
|
||||
// 进行下一轮选择
|
||||
vector *nextChoices = newVector();
|
||||
vectorPushback(nextChoices, choice->left, sizeof(TreeNode));
|
||||
vectorPushback(nextChoices, choice->right, sizeof(TreeNode));
|
||||
backtrack(state, nextChoices, res);
|
||||
TreeNode *nextChoices[2] = {choice->left, choice->right};
|
||||
backtrack(nextChoices);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
undoChoice(state, choice);
|
||||
undoChoice();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1688,7 +1677,7 @@ comments: true
|
|||
| 约束条件 Constraint | 约束条件是问题中限制解的可行性的条件,通常用于剪枝 | 路径中不包含节点 $3$ |
|
||||
| 状态 State | 状态表示问题在某一时刻的情况,包括已经做出的选择 | 当前已访问的节点路径,即 `path` 节点列表 |
|
||||
| 尝试 Attempt | 尝试是根据可用选择来探索解空间的过程,包括做出选择,更新状态,检查是否为解 | 递归访问左(右)子节点,将节点添加进 `path` ,判断节点的值是否为 $7$ |
|
||||
| 回退 Backtracking | 回退指遇到不满足约束条件的状态时,撤销前面做出的选择,回到上一个状态 | 当越过叶结点、结束结点访问、遇到值为 $3$ 的节点时终止搜索,函数返回 |
|
||||
| 回退 Backtracking | 回退指遇到不满足约束条件的状态时,撤销前面做出的选择,回到上一个状态 | 当越过叶节点、结束节点访问、遇到值为 $3$ 的节点时终止搜索,函数返回 |
|
||||
| 剪枝 Pruning | 剪枝是根据问题特性和约束条件避免无意义的搜索路径的方法,可提高搜索效率 | 当遇到值为 $3$ 的节点时,则终止继续搜索 |
|
||||
|
||||
</div>
|
||||
|
|
|
@ -389,38 +389,34 @@ comments: true
|
|||
|
||||
```c title="subset_sum_i_naive.c"
|
||||
/* 回溯算法:子集和 I */
|
||||
void backtrack(vector *state, int target, int total, vector *choices, vector *res) {
|
||||
void backtrack(int target, int total, int *choices, int choicesSize) {
|
||||
// 子集和等于 target 时,记录解
|
||||
if (total == target) {
|
||||
vector *tmpVector = newVector();
|
||||
for (int i = 0; i < state->size; i++) {
|
||||
vectorPushback(tmpVector, state->data[i], sizeof(int));
|
||||
for (int i = 0; i < stateSize; i++) {
|
||||
res[resSize][i] = state[i];
|
||||
}
|
||||
vectorPushback(res, tmpVector, sizeof(vector));
|
||||
resColSizes[resSize++] = stateSize;
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
for (size_t i = 0; i < choices->size; i++) {
|
||||
for (int i = 0; i < choicesSize; i++) {
|
||||
// 剪枝:若子集和超过 target ,则跳过该选择
|
||||
if (total + *(int *)(choices->data[i]) > target) {
|
||||
if (total + choices[i] > target) {
|
||||
continue;
|
||||
}
|
||||
// 尝试:做出选择,更新元素和 total
|
||||
vectorPushback(state, choices->data[i], sizeof(int));
|
||||
state[stateSize++] = choices[i];
|
||||
// 进行下一轮选择
|
||||
backtrack(state, target, total + *(int *)(choices->data[i]), choices, res);
|
||||
backtrack(target, total + choices[i], choices, choicesSize);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
vectorPopback(state);
|
||||
stateSize--;
|
||||
}
|
||||
}
|
||||
|
||||
/* 求解子集和 I(包含重复子集) */
|
||||
vector *subsetSumINaive(vector *nums, int target) {
|
||||
vector *state = newVector(); // 状态(子集)
|
||||
int total = 0; // 子集和
|
||||
vector *res = newVector(); // 结果列表(子集列表)
|
||||
backtrack(state, target, total, nums, res);
|
||||
return res;
|
||||
void subsetSumINaive(int *nums, int numsSize, int target) {
|
||||
resSize = 0; // 初始化解的数量为0
|
||||
backtrack(target, 0, nums, numsSize);
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -867,40 +863,38 @@ comments: true
|
|||
|
||||
```c title="subset_sum_i.c"
|
||||
/* 回溯算法:子集和 I */
|
||||
void backtrack(vector *state, int target, vector *choices, int start, vector *res) {
|
||||
void backtrack(int target, int *choices, int choicesSize, int start) {
|
||||
// 子集和等于 target 时,记录解
|
||||
if (target == 0) {
|
||||
vector *tmpVector = newVector();
|
||||
for (int i = 0; i < state->size; i++) {
|
||||
vectorPushback(tmpVector, state->data[i], sizeof(int));
|
||||
for (int i = 0; i < stateSize; ++i) {
|
||||
res[resSize][i] = state[i];
|
||||
}
|
||||
vectorPushback(res, tmpVector, sizeof(vector));
|
||||
resColSizes[resSize++] = stateSize;
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
// 剪枝二:从 start 开始遍历,避免生成重复子集
|
||||
for (int i = start; i < choices->size; i++) {
|
||||
// 剪枝:若子集和超过 target ,则跳过该选择
|
||||
if (target - *(int *)(choices->data[i]) < 0) {
|
||||
for (int i = start; i < choicesSize; i++) {
|
||||
// 剪枝一:若子集和超过 target ,则直接结束循环
|
||||
// 这是因为数组已排序,后边元素更大,子集和一定超过 target
|
||||
if (target - choices[i] < 0) {
|
||||
break;
|
||||
}
|
||||
// 尝试:做出选择,更新 target, start
|
||||
vectorPushback(state, choices->data[i], sizeof(int));
|
||||
state[stateSize] = choices[i];
|
||||
stateSize++;
|
||||
// 进行下一轮选择
|
||||
backtrack(state, target - *(int *)(choices->data[i]), choices, i, res);
|
||||
backtrack(target - choices[i], choices, choicesSize, i);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
vectorPopback(state);
|
||||
stateSize--;
|
||||
}
|
||||
}
|
||||
|
||||
/* 求解子集和 I */
|
||||
vector *subsetSumI(vector *nums, int target) {
|
||||
vector *state = newVector(); // 状态(子集)
|
||||
qsort(nums->data, nums->size, sizeof(int *), comp); // 对 nums 进行排序
|
||||
int start = 0; // 子集和
|
||||
vector *res = newVector(); // 结果列表(子集列表)
|
||||
backtrack(state, target, nums, start, res);
|
||||
return res;
|
||||
void subsetSumI(int *nums, int numsSize, int target) {
|
||||
qsort(nums, numsSize, sizeof(int), cmp); // 对 nums 进行排序
|
||||
int start = 0; // 遍历起始点
|
||||
backtrack(target, nums, numsSize, start);
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -1383,46 +1377,43 @@ comments: true
|
|||
|
||||
```c title="subset_sum_ii.c"
|
||||
/* 回溯算法:子集和 II */
|
||||
void backtrack(vector *state, int target, vector *choices, int start, vector *res) {
|
||||
void backtrack(int target, int *choices, int choicesSize, int start) {
|
||||
// 子集和等于 target 时,记录解
|
||||
if (target == 0) {
|
||||
vector *tmpVector = newVector();
|
||||
for (int i = 0; i < state->size; i++) {
|
||||
vectorPushback(tmpVector, state->data[i], sizeof(int));
|
||||
for (int i = 0; i < stateSize; i++) {
|
||||
res[resSize][i] = state[i];
|
||||
}
|
||||
vectorPushback(res, tmpVector, sizeof(vector));
|
||||
resColSizes[resSize++] = stateSize;
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
// 剪枝二:从 start 开始遍历,避免生成重复子集
|
||||
// 剪枝三:从 start 开始遍历,避免重复选择同一元素
|
||||
for (int i = start; i < choices->size; i++) {
|
||||
// 剪枝一:若子集和超过 target ,则直接结束循环
|
||||
// 这是因为数组已排序,后边元素更大,子集和一定超过 target
|
||||
if (target - *(int *)(choices->data[i]) < 0) {
|
||||
for (int i = start; i < choicesSize; i++) {
|
||||
// 剪枝一:若子集和超过 target ,则直接跳过
|
||||
if (target - choices[i] < 0) {
|
||||
continue;
|
||||
}
|
||||
// 剪枝四:如果该元素与左边元素相等,说明该搜索分支重复,直接跳过
|
||||
if (i > start && *(int *)(choices->data[i]) == *(int *)(choices->data[i - 1])) {
|
||||
if (i > start && choices[i] == choices[i - 1]) {
|
||||
continue;
|
||||
}
|
||||
// 尝试:做出选择,更新 target, start
|
||||
vectorPushback(state, choices->data[i], sizeof(int));
|
||||
state[stateSize] = choices[i];
|
||||
stateSize++;
|
||||
// 进行下一轮选择
|
||||
backtrack(state, target - *(int *)(choices->data[i]), choices, i + 1, res);
|
||||
backtrack(target - choices[i], choices, choicesSize, i + 1);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
vectorPopback(state);
|
||||
stateSize--;
|
||||
}
|
||||
}
|
||||
|
||||
/* 求解子集和 II */
|
||||
vector *subsetSumII(vector *nums, int target) {
|
||||
vector *state = newVector(); // 状态(子集)
|
||||
qsort(nums->data, nums->size, sizeof(int *), comp); // 对 nums 进行排序
|
||||
int start = 0; // 子集和
|
||||
vector *res = newVector(); // 结果列表(子集列表)
|
||||
backtrack(state, target, nums, start, res);
|
||||
return res;
|
||||
void subsetSumII(int *nums, int numsSize, int target) {
|
||||
// 对 nums 进行排序
|
||||
qsort(nums, numsSize, sizeof(int), cmp);
|
||||
// 开始回溯
|
||||
backtrack(target, nums, numsSize, 0);
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
@ -441,36 +441,35 @@ comments: true
|
|||
|
||||
```c title="hanota.c"
|
||||
/* 移动一个圆盘 */
|
||||
void move(vector *src, vector *tar) {
|
||||
void move(int *src, int *srcSize, int *tar, int *tarSize) {
|
||||
// 从 src 顶部拿出一个圆盘
|
||||
int *panTemp = vectorBack(src);
|
||||
int *pan = malloc(sizeof(int));
|
||||
*pan = *panTemp;
|
||||
vectorPopback(src);
|
||||
int pan = src[*srcSize - 1];
|
||||
src[*srcSize - 1] = 0;
|
||||
(*srcSize)--;
|
||||
// 将圆盘放入 tar 顶部
|
||||
vectorPushback(tar, pan, sizeof(int));
|
||||
tar[*tarSize] = pan;
|
||||
(*tarSize)++;
|
||||
}
|
||||
|
||||
/* 求解汉诺塔:问题 f(i) */
|
||||
void dfs(int i, vector *src, vector *buf, vector *tar) {
|
||||
void dfs(int i, int *src, int *srcSize, int *buf, int *bufSize, int *tar, int *tarSize) {
|
||||
// 若 src 只剩下一个圆盘,则直接将其移到 tar
|
||||
if (i == 1) {
|
||||
move(src, tar);
|
||||
move(src, srcSize, tar, tarSize);
|
||||
return;
|
||||
}
|
||||
// 子问题 f(i-1) :将 src 顶部 i-1 个圆盘借助 tar 移到 buf
|
||||
dfs(i - 1, src, tar, buf);
|
||||
dfs(i - 1, src, srcSize, tar, tarSize, buf, bufSize);
|
||||
// 子问题 f(1) :将 src 剩余一个圆盘移到 tar
|
||||
move(src, tar);
|
||||
move(src, srcSize, tar, tarSize);
|
||||
// 子问题 f(i-1) :将 buf 顶部 i-1 个圆盘借助 src 移到 tar
|
||||
dfs(i - 1, buf, src, tar);
|
||||
dfs(i - 1, buf, bufSize, src, srcSize, tar, tarSize);
|
||||
}
|
||||
|
||||
/* 求解汉诺塔 */
|
||||
void solveHanota(vector *A, vector *B, vector *C) {
|
||||
int n = A->size;
|
||||
void solveHanota(int *A, int *ASize, int *B, int *BSize, int *C, int *CSize) {
|
||||
// 将 A 顶部 n 个圆盘借助 B 移到 C
|
||||
dfs(n, A, B, C);
|
||||
dfs(*ASize, A, ASize, B, BSize, C, CSize);
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
@ -2035,7 +2035,7 @@ comments: true
|
|||
printf("Out of range in %s:%d\n", __FILE__, __LINE__);
|
||||
return;
|
||||
}
|
||||
// 遍历待删除顶点的链表,将所有与待删除结点有关的边删除
|
||||
// 遍历待删除顶点的链表,将所有与待删除节点有关的边删除
|
||||
Node *temp = vet->list->head->next;
|
||||
while (temp != 0) {
|
||||
removeLink(temp->val->list, vet); // 删除与该顶点有关的边
|
||||
|
|
|
@ -193,17 +193,17 @@ comments: true
|
|||
```swift title="top_k.swift"
|
||||
/* 基于堆查找数组中最大的 k 个元素 */
|
||||
func topKHeap(nums: [Int], k: Int) -> [Int] {
|
||||
// 将数组的前 k 个元素入堆
|
||||
var heap = Array(nums.prefix(k))
|
||||
// 初始化一个小顶堆,并将前 k 个元素建堆
|
||||
var heap = Heap(nums.prefix(k))
|
||||
// 从第 k+1 个元素开始,保持堆的长度为 k
|
||||
for i in stride(from: k, to: nums.count, by: 1) {
|
||||
// 若当前元素大于堆顶元素,则将堆顶元素出堆、当前元素入堆
|
||||
if nums[i] > heap.first! {
|
||||
heap.removeFirst()
|
||||
heap.insert(nums[i], at: 0)
|
||||
if nums[i] > heap.min()! {
|
||||
_ = heap.removeMin()
|
||||
heap.insert(nums[i])
|
||||
}
|
||||
}
|
||||
return heap
|
||||
return heap.unordered
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
@ -292,38 +292,39 @@ comments: true
|
|||
|
||||
```swift title="merge_sort.swift"
|
||||
/* 合并左子数组和右子数组 */
|
||||
// 左子数组区间 [left, mid]
|
||||
// 右子数组区间 [mid + 1, right]
|
||||
func merge(nums: inout [Int], left: Int, mid: Int, right: Int) {
|
||||
// 初始化辅助数组
|
||||
let tmp = Array(nums[left ..< (right + 1)])
|
||||
// 左子数组的起始索引和结束索引
|
||||
let leftStart = left - left
|
||||
let leftEnd = mid - left
|
||||
// 右子数组的起始索引和结束索引
|
||||
let rightStart = mid + 1 - left
|
||||
let rightEnd = right - left
|
||||
// i, j 分别指向左子数组、右子数组的首元素
|
||||
var i = leftStart
|
||||
var j = rightStart
|
||||
// 通过覆盖原数组 nums 来合并左子数组和右子数组
|
||||
for k in left ... right {
|
||||
// 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
|
||||
if i > leftEnd {
|
||||
nums[k] = tmp[j]
|
||||
j += 1
|
||||
}
|
||||
// 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++
|
||||
else if j > rightEnd || tmp[i] <= tmp[j] {
|
||||
nums[k] = tmp[i]
|
||||
// 左子数组区间 [left, mid], 右子数组区间 [mid+1, right]
|
||||
// 创建一个临时数组 tmp ,用于存放合并后的结果
|
||||
var tmp = Array(repeating: 0, count: right - left + 1)
|
||||
// 初始化左子数组和右子数组的起始索引
|
||||
var i = left, j = mid + 1, k = 0
|
||||
// 当左右子数组都还有元素时,比较并将较小的元素复制到临时数组中
|
||||
while i <= mid, j <= right {
|
||||
if nums[i] <= nums[j] {
|
||||
tmp[k] = nums[i]
|
||||
i += 1
|
||||
}
|
||||
// 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
|
||||
else {
|
||||
nums[k] = tmp[j]
|
||||
k += 1
|
||||
} else {
|
||||
tmp[k] = nums[j]
|
||||
j += 1
|
||||
k += 1
|
||||
}
|
||||
}
|
||||
// 将左子数组和右子数组的剩余元素复制到临时数组中
|
||||
while i <= mid {
|
||||
tmp[k] = nums[i]
|
||||
i += 1
|
||||
k += 1
|
||||
}
|
||||
while j <= right {
|
||||
tmp[k] = nums[j]
|
||||
j += 1
|
||||
k += 1
|
||||
}
|
||||
// 将临时数组 tmp 中的元素复制回原数组 nums 的对应区间
|
||||
for k in tmp.indices {
|
||||
nums[left + k] = tmp[k]
|
||||
}
|
||||
}
|
||||
|
||||
/* 归并排序 */
|
||||
|
|
|
@ -1067,21 +1067,24 @@ comments: true
|
|||
=== "C"
|
||||
|
||||
```c title="array_binary_tree.c"
|
||||
/* 数组表示下的二叉树类 */
|
||||
/* 数组表示下的二叉树结构体 */
|
||||
typedef struct {
|
||||
vector *tree;
|
||||
int *tree;
|
||||
int size;
|
||||
} ArrayBinaryTree;
|
||||
|
||||
/* 构造函数 */
|
||||
ArrayBinaryTree *newArrayBinaryTree(vector *arr) {
|
||||
ArrayBinaryTree *newABT = malloc(sizeof(ArrayBinaryTree));
|
||||
newABT->tree = arr;
|
||||
return newABT;
|
||||
ArrayBinaryTree *createArrayBinaryTree(int *arr, int arrSize) {
|
||||
ArrayBinaryTree *abt = (ArrayBinaryTree *)malloc(sizeof(ArrayBinaryTree));
|
||||
abt->tree = malloc(sizeof(int) * arrSize);
|
||||
memcpy(abt->tree, arr, sizeof(int) * arrSize);
|
||||
abt->size = arrSize;
|
||||
return abt;
|
||||
}
|
||||
|
||||
/* 节点数量 */
|
||||
int size(ArrayBinaryTree *abt) {
|
||||
return abt->tree->size;
|
||||
return abt->size;
|
||||
}
|
||||
|
||||
/* 获取索引为 i 节点的值 */
|
||||
|
@ -1089,64 +1092,64 @@ comments: true
|
|||
// 若索引越界,则返回 INT_MAX ,代表空位
|
||||
if (i < 0 || i >= size(abt))
|
||||
return INT_MAX;
|
||||
return *(int *)abt->tree->data[i];
|
||||
return abt->tree[i];
|
||||
}
|
||||
|
||||
/* 层序遍历 */
|
||||
int *levelOrder(ArrayBinaryTree *abt, int *returnSize) {
|
||||
int *res = (int *)malloc(sizeof(int) * size(abt));
|
||||
int index = 0;
|
||||
// 直接遍历数组
|
||||
for (int i = 0; i < size(abt); i++) {
|
||||
if (val(abt, i) != INT_MAX)
|
||||
res[index++] = val(abt, i);
|
||||
}
|
||||
*returnSize = index;
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 深度优先遍历 */
|
||||
void dfs(ArrayBinaryTree *abt, int i, const char *order, vector *res) {
|
||||
void dfs(ArrayBinaryTree *abt, int i, char *order, int *res, int *index) {
|
||||
// 若为空位,则返回
|
||||
if (val(abt, i) == INT_MAX)
|
||||
return;
|
||||
// 前序遍历
|
||||
if (strcmp(order, "pre") == 0) {
|
||||
int tmp = val(abt, i);
|
||||
vectorPushback(res, &tmp, sizeof(tmp));
|
||||
}
|
||||
dfs(abt, left(i), order, res);
|
||||
if (strcmp(order, "pre") == 0)
|
||||
res[(*index)++] = val(abt, i);
|
||||
dfs(abt, left(i), order, res, index);
|
||||
// 中序遍历
|
||||
if (strcmp(order, "in") == 0) {
|
||||
int tmp = val(abt, i);
|
||||
vectorPushback(res, &tmp, sizeof(tmp));
|
||||
}
|
||||
dfs(abt, right(i), order, res);
|
||||
if (strcmp(order, "in") == 0)
|
||||
res[(*index)++] = val(abt, i);
|
||||
dfs(abt, right(i), order, res, index);
|
||||
// 后序遍历
|
||||
if (strcmp(order, "post") == 0) {
|
||||
int tmp = val(abt, i);
|
||||
vectorPushback(res, &tmp, sizeof(tmp));
|
||||
}
|
||||
}
|
||||
|
||||
/* 层序遍历 */
|
||||
vector *levelOrder(ArrayBinaryTree *abt) {
|
||||
vector *res = newVector();
|
||||
// 直接遍历数组
|
||||
for (int i = 0; i < size(abt); i++) {
|
||||
if (val(abt, i) != INT_MAX) {
|
||||
int tmp = val(abt, i);
|
||||
vectorPushback(res, &tmp, sizeof(int));
|
||||
}
|
||||
}
|
||||
return res;
|
||||
if (strcmp(order, "post") == 0)
|
||||
res[(*index)++] = val(abt, i);
|
||||
}
|
||||
|
||||
/* 前序遍历 */
|
||||
vector *preOrder(ArrayBinaryTree *abt) {
|
||||
vector *res = newVector();
|
||||
dfs(abt, 0, "pre", res);
|
||||
int *preOrder(ArrayBinaryTree *abt, int *returnSize) {
|
||||
int *res = (int *)malloc(sizeof(int) * size(abt));
|
||||
int index = 0;
|
||||
dfs(abt, 0, "pre", res, &index);
|
||||
*returnSize = index;
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 中序遍历 */
|
||||
vector *inOrder(ArrayBinaryTree *abt) {
|
||||
vector *res = newVector();
|
||||
dfs(abt, 0, "in", res);
|
||||
int *inOrder(ArrayBinaryTree *abt, int *returnSize) {
|
||||
int *res = (int *)malloc(sizeof(int) * size(abt));
|
||||
int index = 0;
|
||||
dfs(abt, 0, "in", res, &index);
|
||||
*returnSize = index;
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 后序遍历 */
|
||||
vector *postOrder(ArrayBinaryTree *abt) {
|
||||
vector *res = newVector();
|
||||
dfs(abt, 0, "post", res);
|
||||
int *postOrder(ArrayBinaryTree *abt, int *returnSize) {
|
||||
int *res = (int *)malloc(sizeof(int) * size(abt));
|
||||
int index = 0;
|
||||
dfs(abt, 0, "post", res, &index);
|
||||
*returnSize = index;
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
|
|
@ -227,19 +227,19 @@ comments: true
|
|||
```rust title="binary_tree_bfs.rs"
|
||||
/* 层序遍历 */
|
||||
fn level_order(root: &Rc<RefCell<TreeNode>>) -> Vec<i32> {
|
||||
// 初始化队列,加入根结点
|
||||
// 初始化队列,加入根节点
|
||||
let mut que = VecDeque::new();
|
||||
que.push_back(Rc::clone(&root));
|
||||
// 初始化一个列表,用于保存遍历序列
|
||||
let mut vec = Vec::new();
|
||||
|
||||
while let Some(node) = que.pop_front() { // 队列出队
|
||||
vec.push(node.borrow().val); // 保存结点值
|
||||
vec.push(node.borrow().val); // 保存节点值
|
||||
if let Some(left) = node.borrow().left.as_ref() {
|
||||
que.push_back(Rc::clone(left)); // 左子结点入队
|
||||
que.push_back(Rc::clone(left)); // 左子节点入队
|
||||
}
|
||||
if let Some(right) = node.borrow().right.as_ref() {
|
||||
que.push_back(Rc::clone(right)); // 右子结点入队
|
||||
que.push_back(Rc::clone(right)); // 右子节点入队
|
||||
};
|
||||
}
|
||||
vec
|
||||
|
@ -654,7 +654,7 @@ comments: true
|
|||
let mut result = vec![];
|
||||
|
||||
if let Some(node) = root {
|
||||
// 访问优先级:根结点 -> 左子树 -> 右子树
|
||||
// 访问优先级:根节点 -> 左子树 -> 右子树
|
||||
result.push(node.borrow().val);
|
||||
result.append(&mut pre_order(node.borrow().left.as_ref()));
|
||||
result.append(&mut pre_order(node.borrow().right.as_ref()));
|
||||
|
@ -667,7 +667,7 @@ comments: true
|
|||
let mut result = vec![];
|
||||
|
||||
if let Some(node) = root {
|
||||
// 访问优先级:左子树 -> 根结点 -> 右子树
|
||||
// 访问优先级:左子树 -> 根节点 -> 右子树
|
||||
result.append(&mut in_order(node.borrow().left.as_ref()));
|
||||
result.push(node.borrow().val);
|
||||
result.append(&mut in_order(node.borrow().right.as_ref()));
|
||||
|
@ -680,7 +680,7 @@ comments: true
|
|||
let mut result = vec![];
|
||||
|
||||
if let Some(node) = root {
|
||||
// 访问优先级:左子树 -> 右子树 -> 根结点
|
||||
// 访问优先级:左子树 -> 右子树 -> 根节点
|
||||
result.append(&mut post_order(node.borrow().left.as_ref()));
|
||||
result.append(&mut post_order(node.borrow().right.as_ref()));
|
||||
result.push(node.borrow().val);
|
||||
|
|
|
@ -30,7 +30,7 @@ comments: true
|
|||
|
||||
!!! question "为什么 DFS 遍历二叉树有前、中、后三种顺序,分别有什么用呢?"
|
||||
|
||||
DFS 的前、中、后序遍历和访问数组的顺序类似,是遍历二叉树的基本方法,利用这三种遍历方法,我们可以得到一个特定顺序的遍历结果。例如在二叉搜索树中,由于结点大小满足 `左子结点值 < 根结点值 < 右子结点值` ,因此我们只要按照 `左->根->右` 的优先级遍历树,就可以获得有序的节点序列。
|
||||
DFS 的前、中、后序遍历和访问数组的顺序类似,是遍历二叉树的基本方法,利用这三种遍历方法,我们可以得到一个特定顺序的遍历结果。例如在二叉搜索树中,由于节点大小满足 `左子节点值 < 根节点值 < 右子节点值` ,因此我们只要按照 `左->根->右` 的优先级遍历树,就可以获得有序的节点序列。
|
||||
|
||||
!!! question "右旋操作是处理失衡节点 `node`、`child`、`grand_child` 之间的关系,那 `node` 的父节点和 `node` 原来的连接不需要维护吗?右旋操作后岂不是断掉了?"
|
||||
|
||||
|
@ -38,7 +38,7 @@ comments: true
|
|||
|
||||
!!! question "在 C++ 中,函数被划分到 `private` 和 `public` 中,这方面有什么考量吗?为什么要将 `height()` 函数和 `updateHeight()` 函数分别放在 `public` 和 `private` 中呢?"
|
||||
|
||||
主要看方法的使用范围,如果方法只在类内部使用,那么就设计为 `private` 。例如,用户单独调用 `updateHeight()` 是没有意义的,它只是插入、删除操作中的一步。而 `height()` 是访问结点高度,类似于 `vector.size()` ,因此设置成 `public` 以便使用。
|
||||
主要看方法的使用范围,如果方法只在类内部使用,那么就设计为 `private` 。例如,用户单独调用 `updateHeight()` 是没有意义的,它只是插入、删除操作中的一步。而 `height()` 是访问节点高度,类似于 `vector.size()` ,因此设置成 `public` 以便使用。
|
||||
|
||||
!!! question "请问如何从一组输入数据构建一个二叉搜索树?根节点的选择是不是很重要?"
|
||||
|
||||
|
|
Loading…
Reference in a new issue