mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-14 16:16:06 +00:00
build
This commit is contained in:
@@ -332,7 +332,28 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="preorder_traversal_ii_compact.c"
|
||||
[class]{}-[func]{preOrder}
|
||||
/* 前序遍历:例题二 */
|
||||
void preOrder(TreeNode *root, vector *path, vector *res) {
|
||||
if (root == NULL) {
|
||||
return;
|
||||
}
|
||||
// 尝试
|
||||
vectorPushback(path, root);
|
||||
if (root->val == 7) {
|
||||
// 记录解
|
||||
vector *newPath = newVector();
|
||||
for (int i = 0; i < path->size; i++) {
|
||||
vectorPushback(newPath, path->data[i]);
|
||||
}
|
||||
vectorPushback(res, newPath);
|
||||
}
|
||||
|
||||
preOrder(root->left, path, res);
|
||||
preOrder(root->right, path, res);
|
||||
|
||||
// 回退
|
||||
vectorPopback(path);
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -607,7 +628,30 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="preorder_traversal_iii_compact.c"
|
||||
[class]{}-[func]{preOrder}
|
||||
/* 前序遍历:例题三 */
|
||||
void preOrder(TreeNode *root, vector *path, vector *res) {
|
||||
// 剪枝
|
||||
if (root == NULL || root->val == 3) {
|
||||
return;
|
||||
}
|
||||
// 尝试
|
||||
vectorPushback(path, root);
|
||||
if (root->val == 7) {
|
||||
// 记录解
|
||||
vector *newPath = newVector();
|
||||
for (int i = 0; i < path->size; i++) {
|
||||
vectorPushback(newPath, path->data[i]);
|
||||
}
|
||||
vectorPushback(res, newPath);
|
||||
res->depth++;
|
||||
}
|
||||
|
||||
preOrder(root->left, path, res);
|
||||
preOrder(root->right, path, res);
|
||||
|
||||
// 回退
|
||||
vectorPopback(path);
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -1281,17 +1325,60 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="preorder_traversal_iii_template.c"
|
||||
[class]{}-[func]{isSolution}
|
||||
/* 判断当前状态是否为解 */
|
||||
bool isSolution(vector *state) {
|
||||
return state->size != 0 && ((TreeNode *)(state->data[state->size - 1]))->val == 7;
|
||||
}
|
||||
|
||||
[class]{}-[func]{recordSolution}
|
||||
/* 记录解 */
|
||||
void recordSolution(vector *state, vector *res) {
|
||||
vector *newPath = newVector();
|
||||
for (int i = 0; i < state->size; i++) {
|
||||
vectorPushback(newPath, state->data[i]);
|
||||
}
|
||||
vectorPushback(res, newPath);
|
||||
}
|
||||
|
||||
[class]{}-[func]{isValid}
|
||||
/* 判断在当前状态下,该选择是否合法 */
|
||||
bool isValid(vector *state, TreeNode *choice) {
|
||||
return choice != NULL && choice->val != 3;
|
||||
}
|
||||
|
||||
[class]{}-[func]{makeChoice}
|
||||
/* 更新状态 */
|
||||
void makeChoice(vector *state, TreeNode *choice) {
|
||||
vectorPushback(state, choice);
|
||||
}
|
||||
|
||||
[class]{}-[func]{undoChoice}
|
||||
/* 恢复状态 */
|
||||
void undoChoice(vector *state, TreeNode *choice) {
|
||||
vectorPopback(state);
|
||||
}
|
||||
|
||||
[class]{}-[func]{backtrack}
|
||||
/* 前序遍历:例题三 */
|
||||
void backtrack(vector *state, vector *choices, vector *res) {
|
||||
// 检查是否为解
|
||||
if (isSolution(state)) {
|
||||
// 记录解
|
||||
recordSolution(state, res);
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
for (int i = 0; i < choices->size; i++) {
|
||||
TreeNode *choice = choices->data[i];
|
||||
// 剪枝:检查选择是否合法
|
||||
if (isValid(state, choice)) {
|
||||
// 尝试:做出选择,更新状态
|
||||
makeChoice(state, choice);
|
||||
// 进行下一轮选择
|
||||
vector *nextChoices = newVector();
|
||||
vectorPushback(nextChoices, choice->left);
|
||||
vectorPushback(nextChoices, choice->right);
|
||||
backtrack(state, nextChoices, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
undoChoice(state, choice);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
@@ -269,9 +269,52 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="permutations_i.c"
|
||||
[class]{}-[func]{backtrack}
|
||||
/* 回溯算法:全排列 I */
|
||||
void backtrack(vector *state, vector *choices, vector *selected, vector *res) {
|
||||
// 当状态长度等于元素数量时,记录解
|
||||
if (state->size == choices->size) {
|
||||
vector *newState = newVector();
|
||||
for (int i = 0; i < state->size; i++) {
|
||||
vectorPushback(newState, state->data[i]);
|
||||
}
|
||||
vectorPushback(res, newState);
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
for (int i = 0; i < choices->size; i++) {
|
||||
int *choice = malloc(sizeof(int));
|
||||
*choice = *((int *)(choices->data[i]));
|
||||
// 剪枝:不允许重复选择元素 且 不允许重复选择相等元素
|
||||
bool select = *((bool *)(selected->data[i]));
|
||||
if (!select) {
|
||||
// 尝试:做出选择,更新状态
|
||||
*((bool *)selected->data[i]) = true;
|
||||
vectorPushback(state, choice);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, choices, selected, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
*((bool *)selected->data[i]) = false;
|
||||
vectorPopback(state);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[class]{}-[func]{permutationsI}
|
||||
/* 全排列 I */
|
||||
vector *permutationsI(vector *nums) {
|
||||
vector *iState = newVector();
|
||||
|
||||
int select[3] = {false, false, false};
|
||||
vector *bSelected = newVector();
|
||||
for (int i = 0; i < nums->size; i++) {
|
||||
vectorPushback(bSelected, &select[i]);
|
||||
}
|
||||
|
||||
vector *res = newVector();
|
||||
|
||||
// 前序遍历
|
||||
backtrack(iState, nums, bSelected, res);
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
@@ -182,9 +182,40 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="subset_sum_i_naive.c"
|
||||
[class]{}-[func]{backtrack}
|
||||
/* 回溯算法:子集和 I */
|
||||
void backtrack(vector *state, int target, int total, vector *choices, vector *res) {
|
||||
// 子集和等于 target 时,记录解
|
||||
if (total == target) {
|
||||
vector *tmpVector = newVector();
|
||||
for (int i = 0; i < state->size; i++) {
|
||||
vectorPushback(tmpVector, state->data[i]);
|
||||
}
|
||||
vectorPushback(res, tmpVector);
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
for (size_t i = 0; i < choices->size; i++) {
|
||||
// 剪枝:若子集和超过 target ,则跳过该选择
|
||||
if (total + *(int *)(choices->data[i]) > target) {
|
||||
continue;
|
||||
}
|
||||
// 尝试:做出选择,更新元素和 total
|
||||
vectorPushback(state, choices->data[i]);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, target, total + *(int *)(choices->data[i]), choices, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
vectorPopback(state);
|
||||
}
|
||||
}
|
||||
|
||||
[class]{}-[func]{subsetSumINaive}
|
||||
/* 求解子集和 I(包含重复子集) */
|
||||
vector *subsetSumINaive(vector *nums, int target) {
|
||||
vector *state = newVector(); // 状态(子集)
|
||||
int total = 0; // 子集和
|
||||
vector *res = newVector(); // 结果列表(子集列表)
|
||||
backtrack(state, target, total, nums, res);
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -518,9 +549,42 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="subset_sum_i.c"
|
||||
[class]{}-[func]{backtrack}
|
||||
/* 回溯算法:子集和 I */
|
||||
void backtrack(vector *state, int target, vector *choices, int start, vector *res) {
|
||||
// 子集和等于 target 时,记录解
|
||||
if (target == 0) {
|
||||
vector *tmpVector = newVector();
|
||||
for (int i = 0; i < state->size; i++) {
|
||||
vectorPushback(tmpVector, state->data[i]);
|
||||
}
|
||||
vectorPushback(res, tmpVector);
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
// 剪枝二:从 start 开始遍历,避免生成重复子集
|
||||
for (int i = start; i < choices->size; i++) {
|
||||
// 剪枝:若子集和超过 target ,则跳过该选择
|
||||
if (target - *(int *)(choices->data[i]) < 0) {
|
||||
continue;
|
||||
}
|
||||
// 尝试:做出选择,更新 target, start
|
||||
vectorPushback(state, choices->data[i]);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, target - *(int *)(choices->data[i]), choices, i, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
vectorPopback(state);
|
||||
}
|
||||
}
|
||||
|
||||
[class]{}-[func]{subsetSumI}
|
||||
/* 求解子集和 I */
|
||||
vector *subsetSumI(vector *nums, int target) {
|
||||
vector *state = newVector(); // 状态(子集)
|
||||
qsort(nums->data[0], nums->size, sizeof(int), comp); // 对 nums 进行排序
|
||||
int start = 0; // 子集和
|
||||
vector *res = newVector(); // 结果列表(子集列表)
|
||||
backtrack(state, target, nums, start, res);
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -869,9 +933,48 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="subset_sum_ii.c"
|
||||
[class]{}-[func]{backtrack}
|
||||
/* 回溯算法:子集和 II */
|
||||
void backtrack(vector *state, int target, vector *choices, int start, vector *res) {
|
||||
// 子集和等于 target 时,记录解
|
||||
if (target == 0) {
|
||||
vector *tmpVector = newVector();
|
||||
for (int i = 0; i < state->size; i++) {
|
||||
vectorPushback(tmpVector, state->data[i]);
|
||||
}
|
||||
vectorPushback(res, tmpVector);
|
||||
return;
|
||||
}
|
||||
// 遍历所有选择
|
||||
// 剪枝二:从 start 开始遍历,避免生成重复子集
|
||||
// 剪枝三:从 start 开始遍历,避免重复选择同一元素
|
||||
for (int i = start; i < choices->size; i++) {
|
||||
// 剪枝一:若子集和超过 target ,则直接结束循环
|
||||
// 这是因为数组已排序,后边元素更大,子集和一定超过 target
|
||||
if (target - *(int *)(choices->data[i]) < 0) {
|
||||
continue;
|
||||
}
|
||||
// 剪枝四:如果该元素与左边元素相等,说明该搜索分支重复,直接跳过
|
||||
if (i > start && *(int *)(choices->data[i]) == *(int *)(choices->data[i - 1])) {
|
||||
continue;
|
||||
}
|
||||
// 尝试:做出选择,更新 target, start
|
||||
vectorPushback(state, choices->data[i]);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, target - *(int *)(choices->data[i]), choices, i + 1, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
vectorPopback(state);
|
||||
}
|
||||
}
|
||||
|
||||
[class]{}-[func]{subsetSumII}
|
||||
/* 求解子集和 II */
|
||||
vector *subsetSumII(vector *nums, int target) {
|
||||
vector *state = newVector(); // 状态(子集)
|
||||
qsort(nums->data[0], nums->size, sizeof(int), comp); // 对 nums 进行排序
|
||||
int start = 0; // 子集和
|
||||
vector *res = newVector(); // 结果列表(子集列表)
|
||||
backtrack(state, target, nums, start, res);
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
Reference in New Issue
Block a user