mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-03 18:50:58 +08:00
refactor: Replace vector with array in C code (#894)
* Re-implement merge sort function. * Replace vector with array for C. * fix
This commit is contained in:
@@ -6,22 +6,20 @@
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
vector *res;
|
||||
// 假设结果长度不超过 100
|
||||
#define MAX_SIZE 100
|
||||
|
||||
// 打印向量中的元素
|
||||
void printFunc(vector *v, void *p) {
|
||||
TreeNode *node = p;
|
||||
printf("%d ", node->val);
|
||||
}
|
||||
TreeNode *res[MAX_SIZE];
|
||||
int resSize = 0;
|
||||
|
||||
/* 前序遍历:例题一 */
|
||||
void preOrder(TreeNode *root) {
|
||||
static void preOrder(TreeNode *root) {
|
||||
if (root == NULL) {
|
||||
return;
|
||||
}
|
||||
if (root->val == 7) {
|
||||
// 记录解
|
||||
vectorPushback(res, root, sizeof(int));
|
||||
res[resSize++] = root;
|
||||
}
|
||||
preOrder(root->left);
|
||||
preOrder(root->right);
|
||||
@@ -30,15 +28,21 @@ void preOrder(TreeNode *root) {
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int arr[] = {1, 7, 3, 4, 5, 6, 7};
|
||||
res = newVector();
|
||||
TreeNode *root = arrToTree(arr, sizeof(arr) / sizeof(arr[0]));
|
||||
printf("\n初始化二叉树\r\n");
|
||||
TreeNode *root = arrayToTree(arr, sizeof(arr) / sizeof(arr[0]));
|
||||
printf("\n初始化二叉树\n");
|
||||
printTree(root);
|
||||
|
||||
// 前序遍历
|
||||
preOrder(root);
|
||||
|
||||
printf("\n输出所有值为 7 的节点\r\n");
|
||||
printVector(res, printFunc);
|
||||
delVector(res);
|
||||
}
|
||||
printf("\n输出所有值为 7 的节点\n");
|
||||
int vals[resSize];
|
||||
for (int i = 0; i < resSize; i++) {
|
||||
vals[i] = res[i]->val;
|
||||
}
|
||||
printArray(vals, resSize);
|
||||
|
||||
// 释放内存
|
||||
freeMemoryTree(root);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,62 +6,55 @@
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
// 假设路径和结果长度不超过 100
|
||||
#define MAX_SIZE 100
|
||||
#define MAX_RES_SIZE 100
|
||||
|
||||
TreeNode *path[MAX_SIZE];
|
||||
TreeNode *res[MAX_RES_SIZE][MAX_SIZE];
|
||||
int pathSize = 0, resSize = 0;
|
||||
|
||||
/* 前序遍历:例题二 */
|
||||
void preOrder(TreeNode *root, vector *path, vector *res) {
|
||||
static 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);
|
||||
}
|
||||
|
||||
// 打印向量中的元素
|
||||
void printResult(vector *vv) {
|
||||
for (int i = 0; i < vv->size; i++) {
|
||||
vector *v = (vector *)vv->data[i];
|
||||
for (int j = 0; j < v->size; j++) {
|
||||
TreeNode *node = (TreeNode *)v->data[j];
|
||||
printf("%d ", node->val);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
pathSize--;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int arr[] = {1, 7, 3, 4, 5, 6, 7};
|
||||
int n = sizeof(arr) / sizeof(arr[0]);
|
||||
TreeNode *root = arrToTree(arr, n);
|
||||
printf("\r\n初始化二叉树\r\n");
|
||||
TreeNode *root = arrayToTree(arr, sizeof(arr) / sizeof(arr[0]));
|
||||
printf("\n初始化二叉树\n");
|
||||
printTree(root);
|
||||
|
||||
// 创建存储路径和结果的向量
|
||||
vector *path = newVector();
|
||||
vector *res = newVector();
|
||||
|
||||
// 前序遍历
|
||||
preOrder(root, path, res);
|
||||
preOrder(root);
|
||||
|
||||
// 输出结果
|
||||
printf("输出所有根节点到节点 7 的路径:\n");
|
||||
printResult(res);
|
||||
printf("\n输出所有根节点到节点 7 的路径\n");
|
||||
for (int i = 0; i < resSize; ++i) {
|
||||
int vals[MAX_SIZE];
|
||||
int size = 0;
|
||||
for (int j = 0; res[i][j] != NULL; ++j) {
|
||||
vals[size++] = res[i][j]->val;
|
||||
}
|
||||
printArray(vals, size);
|
||||
}
|
||||
|
||||
// 释放内存
|
||||
delVector(path);
|
||||
delVector(res);
|
||||
freeMemoryTree(root);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,64 +6,56 @@
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
// 假设路径和结果长度不超过 100
|
||||
#define MAX_SIZE 100
|
||||
#define MAX_RES_SIZE 100
|
||||
|
||||
TreeNode *path[MAX_SIZE];
|
||||
TreeNode *res[MAX_RES_SIZE][MAX_SIZE];
|
||||
int pathSize = 0, resSize = 0;
|
||||
|
||||
/* 前序遍历:例题三 */
|
||||
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);
|
||||
}
|
||||
|
||||
// 打印向量中的元素
|
||||
void printResult(vector *vv) {
|
||||
for (int i = 0; i < vv->size; i++) {
|
||||
vector *v = (vector *)vv->data[i];
|
||||
for (int j = 0; j < v->size; j++) {
|
||||
TreeNode *node = (TreeNode *)v->data[j];
|
||||
printf("%d ", node->val);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
pathSize--;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int arr[] = {1, 7, 3, 4, 5, 6, 7};
|
||||
int n = sizeof(arr) / sizeof(arr[0]);
|
||||
TreeNode *root = arrToTree(arr, n);
|
||||
printf("\r\n初始化二叉树\r\n");
|
||||
TreeNode *root = arrayToTree(arr, sizeof(arr) / sizeof(arr[0]));
|
||||
printf("\n初始化二叉树\n");
|
||||
printTree(root);
|
||||
|
||||
// 创建存储路径和结果的向量
|
||||
vector *path = newVector();
|
||||
vector *res = newVector();
|
||||
|
||||
// 前序遍历
|
||||
preOrder(root, path, res);
|
||||
preOrder(root);
|
||||
|
||||
// 输出结果
|
||||
printf("输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点:\n");
|
||||
printResult(res);
|
||||
printf("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点\n");
|
||||
for (int i = 0; i < resSize; ++i) {
|
||||
int vals[MAX_SIZE];
|
||||
int size = 0;
|
||||
for (int j = 0; res[i][j] != NULL; ++j) {
|
||||
vals[size++] = res[i][j]->val;
|
||||
}
|
||||
printArray(vals, size);
|
||||
}
|
||||
|
||||
// 释放内存
|
||||
delVector(path);
|
||||
delVector(res);
|
||||
freeMemoryTree(root);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,96 +6,87 @@
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
// 假设路径和结果长度不超过 100
|
||||
#define MAX_SIZE 100
|
||||
#define MAX_RES_SIZE 100
|
||||
|
||||
TreeNode *path[MAX_SIZE];
|
||||
TreeNode *res[MAX_RES_SIZE][MAX_SIZE];
|
||||
int pathSize = 0, resSize = 0;
|
||||
|
||||
/* 判断当前状态是否为解 */
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 打印向量中的元素
|
||||
void printFunc(vector *v, void *p) {
|
||||
TreeNode *node = p;
|
||||
printf("%d ", node->val);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int arr[] = {1, 7, 3, 4, 5, 6, 7};
|
||||
int n = sizeof(arr) / sizeof(arr[0]);
|
||||
TreeNode *root = arrToTree(arr, n);
|
||||
printf("\r\n初始化二叉树\r\n");
|
||||
TreeNode *root = arrayToTree(arr, sizeof(arr) / sizeof(arr[0]));
|
||||
printf("\n初始化二叉树\n");
|
||||
printTree(root);
|
||||
|
||||
// 回溯算法
|
||||
vector *state = newVector();
|
||||
vector *choices = newVector();
|
||||
vector *res = newVector();
|
||||
vectorPushback(choices, root, sizeof(TreeNode));
|
||||
backtrack(state, choices, res);
|
||||
TreeNode *choices[2] = {root, NULL};
|
||||
backtrack(choices);
|
||||
|
||||
printf("输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点:\n");
|
||||
for (int i = 0; i < res->size; i++) {
|
||||
vector *path = res->data[i];
|
||||
vector *vals = newVector();
|
||||
for (int j = 0; j < path->size; j++) {
|
||||
TreeNode *node = path->data[j];
|
||||
vectorPushback(vals, &node->val, sizeof(int));
|
||||
printf("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点\n");
|
||||
for (int i = 0; i < resSize; ++i) {
|
||||
int vals[MAX_SIZE];
|
||||
int size = 0;
|
||||
for (int j = 0; res[i][j] != NULL; ++j) {
|
||||
vals[size++] = res[i][j]->val;
|
||||
}
|
||||
printVector(vals, printFunc);
|
||||
printArray(vals, size);
|
||||
}
|
||||
|
||||
// 释放内存
|
||||
delVector(state);
|
||||
delVector(choices);
|
||||
delVector(res);
|
||||
freeMemoryTree(root);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,73 +6,73 @@
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
#define MAX_SIZE 100
|
||||
#define MAX_RES_SIZE 100
|
||||
|
||||
// 状态(子集)
|
||||
int state[MAX_SIZE];
|
||||
int stateSize = 0;
|
||||
|
||||
// 结果列表(子集列表)
|
||||
int res[MAX_RES_SIZE][MAX_SIZE];
|
||||
int resColSizes[MAX_RES_SIZE];
|
||||
int resSize = 0;
|
||||
|
||||
/* 回溯算法:子集和 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--;
|
||||
}
|
||||
}
|
||||
|
||||
/* 用来做比较的函数 */
|
||||
int comp(const void *a, const void *b) {
|
||||
return *(int *)a - *(int *)b;
|
||||
/* 比较函数 */
|
||||
int cmp(const void *a, const void *b) {
|
||||
return (*(int *)a - *(int *)b);
|
||||
}
|
||||
|
||||
/* 求解子集和 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 printFunc(vector *v, void *p) {
|
||||
int *node = p;
|
||||
printf("%d", *node);
|
||||
void subsetSumI(int *nums, int numsSize, int target) {
|
||||
qsort(nums, numsSize, sizeof(int), cmp); // 对 nums 进行排序
|
||||
int start = 0; // 遍历起始点
|
||||
backtrack(target, nums, numsSize, start);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int nums[] = {3, 4, 5};
|
||||
vector *iNums = newVector();
|
||||
for (int i = 0; i < sizeof(nums) / sizeof(nums[0]); i++) {
|
||||
vectorPushback(iNums, &nums[i], sizeof(int));
|
||||
}
|
||||
|
||||
int numsSize = sizeof(nums) / sizeof(nums[0]);
|
||||
int target = 9;
|
||||
|
||||
vector *res = subsetSumI(iNums, target);
|
||||
subsetSumI(nums, numsSize, target);
|
||||
|
||||
printf("输入数组 nums = ");
|
||||
printVector(iNums, printFunc);
|
||||
printArray(nums, numsSize);
|
||||
printf("target = %d\n", target);
|
||||
printf("所有和等于 %d 的子集 res = \r\n", target);
|
||||
printVectorMatrix(res, printFunc);
|
||||
printf("所有和等于 %d 的子集 res = \n", target);
|
||||
for (int i = 0; i < resSize; ++i) {
|
||||
printArray(res[i], resColSizes[i]);
|
||||
}
|
||||
|
||||
delVector(iNums);
|
||||
delVector(res);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,65 +6,64 @@
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
#define MAX_SIZE 100
|
||||
#define MAX_RES_SIZE 100
|
||||
|
||||
// 状态(子集)
|
||||
int state[MAX_SIZE];
|
||||
int stateSize = 0;
|
||||
|
||||
// 结果列表(子集列表)
|
||||
int res[MAX_RES_SIZE][MAX_SIZE];
|
||||
int resColSizes[MAX_RES_SIZE];
|
||||
int resSize = 0;
|
||||
|
||||
/* 回溯算法:子集和 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 printFunc(vector *v, void *p) {
|
||||
int *node = p;
|
||||
printf("%d", *node);
|
||||
void subsetSumINaive(int *nums, int numsSize, int target) {
|
||||
resSize = 0; // 初始化解的数量为0
|
||||
backtrack(target, 0, nums, numsSize);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int nums[] = {3, 4, 5};
|
||||
vector *iNums = newVector();
|
||||
for (int i = 0; i < sizeof(nums) / sizeof(nums[0]); i++) {
|
||||
vectorPushback(iNums, &nums[i], sizeof(int));
|
||||
}
|
||||
int numsSize = sizeof(nums) / sizeof(nums[0]);
|
||||
int target = 9;
|
||||
|
||||
vector *res = subsetSumINaive(iNums, target);
|
||||
subsetSumINaive(nums, numsSize, target);
|
||||
|
||||
printf("输入数组 nums = ");
|
||||
printVector(iNums, printFunc);
|
||||
printArray(nums, numsSize);
|
||||
printf("target = %d\n", target);
|
||||
printf("所有和等于 %d 的子集 res = \r\n", target);
|
||||
printVectorMatrix(res, printFunc);
|
||||
printf("所有和等于 %d 的子集 res = \n", target);
|
||||
for (int i = 0; i < resSize; i++) {
|
||||
printArray(res[i], resColSizes[i]);
|
||||
}
|
||||
|
||||
delVector(iNums);
|
||||
delVector(res);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,78 +6,78 @@
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
#define MAX_SIZE 100
|
||||
#define MAX_RES_SIZE 100
|
||||
|
||||
// 状态(子集)
|
||||
int state[MAX_SIZE];
|
||||
int stateSize = 0;
|
||||
|
||||
// 结果列表(子集列表)
|
||||
int res[MAX_RES_SIZE][MAX_SIZE];
|
||||
int resColSizes[MAX_RES_SIZE];
|
||||
int resSize = 0;
|
||||
|
||||
/* 回溯算法:子集和 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--;
|
||||
}
|
||||
}
|
||||
|
||||
/* 比较规则 */
|
||||
int comp(const void *a, const void *b) {
|
||||
return *(int *)a - *(int *)b;
|
||||
/* 比较函数 */
|
||||
int cmp(const void *a, const void *b) {
|
||||
return (*(int *)a - *(int *)b);
|
||||
}
|
||||
|
||||
/* 求解子集和 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 printFunc(vector *v, void *p) {
|
||||
int *node = p;
|
||||
printf("%d", *node);
|
||||
void subsetSumII(int *nums, int numsSize, int target) {
|
||||
// 对 nums 进行排序
|
||||
qsort(nums, numsSize, sizeof(int), cmp);
|
||||
// 开始回溯
|
||||
backtrack(target, nums, numsSize, 0);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int nums[] = {4, 4, 5};
|
||||
vector *iNums = newVector();
|
||||
for (int i = 0; i < sizeof(nums) / sizeof(nums[0]); i++) {
|
||||
vectorPushback(iNums, &nums[i], sizeof(int));
|
||||
}
|
||||
int numsSize = sizeof(nums) / sizeof(nums[0]);
|
||||
int target = 9;
|
||||
|
||||
vector *res = subsetSumII(iNums, target);
|
||||
subsetSumII(nums, numsSize, target);
|
||||
|
||||
printf("输入数组 nums = ");
|
||||
printVector(iNums, printFunc);
|
||||
printArray(nums, numsSize);
|
||||
printf("target = %d\n", target);
|
||||
printf("所有和等于 %d 的子集 res = \r\n", target);
|
||||
printVectorMatrix(res, printFunc);
|
||||
printf("所有和等于 %d 的子集 res = \n", target);
|
||||
for (int i = 0; i < resSize; ++i) {
|
||||
printArray(res[i], resColSizes[i]);
|
||||
}
|
||||
|
||||
delVector(iNums);
|
||||
delVector(res);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,21 +6,24 @@
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* 数组表示下的二叉树类 */
|
||||
/* 数组表示下的二叉树结构 */
|
||||
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 节点的值 */
|
||||
@@ -28,7 +31,7 @@ int val(ArrayBinaryTree *abt, int i) {
|
||||
// 若索引越界,则返回 INT_MAX ,代表空位
|
||||
if (i < 0 || i >= size(abt))
|
||||
return INT_MAX;
|
||||
return *(int *)abt->tree->data[i];
|
||||
return abt->tree[i];
|
||||
}
|
||||
|
||||
/* 获取索引为 i 节点的左子节点的索引 */
|
||||
@@ -46,114 +49,114 @@ int parent(int i) {
|
||||
return (i - 1) / 2;
|
||||
}
|
||||
|
||||
/* 层序遍历 */
|
||||
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;
|
||||
}
|
||||
|
||||
/* 打印向量中的元素 */
|
||||
void printFunc(vector *v, void *p) {
|
||||
int *val = p;
|
||||
printf("%d", *val);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
// 初始化二叉树
|
||||
// 使用 INT_MAX 代表空位 nullptr
|
||||
// 使用 INT_MAX 代表空位 NULL
|
||||
int arr[] = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};
|
||||
TreeNode *root = arrToTree(arr, sizeof(arr) / sizeof(arr[0]));
|
||||
int arrSize = sizeof(arr) / sizeof(arr[0]);
|
||||
TreeNode *root = arrayToTree(arr, arrSize);
|
||||
printf("\n初始化二叉树\n");
|
||||
printf("二叉树的数组表示:\n");
|
||||
printArray(arr, sizeof(arr) / sizeof(arr[0]));
|
||||
printArray(arr, arrSize);
|
||||
printf("二叉树的链表表示:\n");
|
||||
printTree(root);
|
||||
|
||||
vector *vArr = newVector();
|
||||
for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) {
|
||||
vectorPushback(vArr, &arr[i], sizeof(int));
|
||||
}
|
||||
// 数组表示下的二叉树类
|
||||
ArrayBinaryTree *abt = newArrayBinaryTree(vArr);
|
||||
ArrayBinaryTree *abt = createArrayBinaryTree(arr, arrSize);
|
||||
|
||||
// 访问节点
|
||||
int i = 1;
|
||||
int l = left(i), r = right(i), p = parent(i);
|
||||
printf("\n当前节点的索引为 %d ,值为 %d\n", i, val(abt, i));
|
||||
printf("其左子节点的索引为 %d ,值为 %d\r\n", l, val(abt, l));
|
||||
printf("其右子节点的索引为 %d ,值为 %d\r\n", r, val(abt, r));
|
||||
printf("其父节点的索引为 %d ,值为 %d\r\n", p, val(abt, p));
|
||||
printf("\n当前节点的索引为 %d,值为 %d\n", i, val(abt, i));
|
||||
printf("其左子节点的索引为 %d,值为 %d\n", l, l < arrSize ? val(abt, l) : INT_MAX);
|
||||
printf("其右子节点的索引为 %d,值为 %d\n", r, r < arrSize ? val(abt, r) : INT_MAX);
|
||||
printf("其父节点的索引为 %d,值为 %d\n", p, p < arrSize ? val(abt, p) : INT_MAX);
|
||||
|
||||
// 遍历树
|
||||
vector *res = levelOrder(abt);
|
||||
int returnSize;
|
||||
int *res;
|
||||
|
||||
res = levelOrder(abt, &returnSize);
|
||||
printf("\n层序遍历为: ");
|
||||
printVector(res, printFunc);
|
||||
delVector(res);
|
||||
res = preOrder(abt);
|
||||
printArray(res, returnSize);
|
||||
free(res);
|
||||
|
||||
res = preOrder(abt, &returnSize);
|
||||
printf("前序遍历为: ");
|
||||
printVector(res, printFunc);
|
||||
delVector(res);
|
||||
res = inOrder(abt);
|
||||
printArray(res, returnSize);
|
||||
free(res);
|
||||
|
||||
res = inOrder(abt, &returnSize);
|
||||
printf("中序遍历为: ");
|
||||
printVector(res, printFunc);
|
||||
delVector(res);
|
||||
res = postOrder(abt);
|
||||
printArray(res, returnSize);
|
||||
free(res);
|
||||
|
||||
res = postOrder(abt, &returnSize);
|
||||
printf("后序遍历为: ");
|
||||
printVector(res, printFunc);
|
||||
delVector(res);
|
||||
printArray(res, returnSize);
|
||||
free(res);
|
||||
|
||||
// 释放内存
|
||||
free(root);
|
||||
free(abt);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user