mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 09:56:29 +08:00
A few improvements to the C code.
This commit is contained in:
parent
0e10274bb4
commit
bd8cda325e
5 changed files with 21 additions and 18 deletions
|
@ -6,12 +6,11 @@
|
|||
|
||||
#include "../utils/common.h"
|
||||
|
||||
#define MAX_N 100
|
||||
#define MAX_RES 1000
|
||||
#define MAX_SIZE 100
|
||||
|
||||
/* 回溯算法:N 皇后 */
|
||||
void backtrack(int row, int n, char state[MAX_N][MAX_N], char ***res, int *resSize, bool cols[MAX_N],
|
||||
bool diags1[2 * MAX_N - 1], bool diags2[2 * MAX_N - 1]) {
|
||||
void backtrack(int row, int n, char state[MAX_SIZE][MAX_SIZE], char ***res, int *resSize, bool cols[MAX_SIZE],
|
||||
bool diags1[2 * MAX_SIZE - 1], bool diags2[2 * MAX_SIZE - 1]) {
|
||||
// 当放置完所有行时,记录解
|
||||
if (row == n) {
|
||||
res[*resSize] = (char **)malloc(sizeof(char *) * n);
|
||||
|
@ -43,7 +42,7 @@ void backtrack(int row, int n, char state[MAX_N][MAX_N], char ***res, int *resSi
|
|||
|
||||
/* 求解 N 皇后 */
|
||||
char ***nQueens(int n, int *returnSize) {
|
||||
char state[MAX_N][MAX_N];
|
||||
char state[MAX_SIZE][MAX_SIZE];
|
||||
// 初始化 n*n 大小的棋盘,其中 'Q' 代表皇后,'#' 代表空位
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 0; j < n; ++j) {
|
||||
|
@ -51,11 +50,11 @@ char ***nQueens(int n, int *returnSize) {
|
|||
}
|
||||
state[i][n] = '\0';
|
||||
}
|
||||
bool cols[MAX_N] = {false}; // 记录列是否有皇后
|
||||
bool diags1[2 * MAX_N - 1] = {false}; // 记录主对角线是否有皇后
|
||||
bool diags2[2 * MAX_N - 1] = {false}; // 记录副对角线是否有皇后
|
||||
bool cols[MAX_SIZE] = {false}; // 记录列是否有皇后
|
||||
bool diags1[2 * MAX_SIZE - 1] = {false}; // 记录主对角线是否有皇后
|
||||
bool diags2[2 * MAX_SIZE - 1] = {false}; // 记录副对角线是否有皇后
|
||||
|
||||
char ***res = (char ***)malloc(sizeof(char **) * MAX_RES);
|
||||
char ***res = (char ***)malloc(sizeof(char **) * MAX_SIZE);
|
||||
*returnSize = 0;
|
||||
backtrack(0, n, state, res, returnSize, cols, diags1, diags2);
|
||||
return res;
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
#include "../utils/common.h"
|
||||
|
||||
// 假设所有元素都小于 1000
|
||||
#define MAX_N 1000
|
||||
#define MAX_SIZE 1000
|
||||
|
||||
/* 构建二叉树:分治 */
|
||||
TreeNode *dfs(int *preorder, int *inorderMap, int i, int l, int r, int size) {
|
||||
|
@ -32,7 +32,7 @@ TreeNode *dfs(int *preorder, int *inorderMap, int i, int l, int r, int size) {
|
|||
/* 构建二叉树 */
|
||||
TreeNode *buildTree(int *preorder, int preorderSize, int *inorder, int inorderSize) {
|
||||
// 初始化哈希表,存储 inorder 元素到索引的映射
|
||||
int *inorderMap = (int *)malloc(sizeof(int) * MAX_N);
|
||||
int *inorderMap = (int *)malloc(sizeof(int) * MAX_SIZE);
|
||||
for (int i = 0; i < inorderSize; i++) {
|
||||
inorderMap[inorder[i]] = i;
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ void selectionSort(int nums[], int n) {
|
|||
}
|
||||
}
|
||||
|
||||
/* 主函数 */
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int nums[] = {4, 1, 3, 1, 5, 2};
|
||||
int n = sizeof(nums) / sizeof(nums[0]);
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
|
||||
#include "../utils/common.h"
|
||||
|
||||
#define MAX_SIZE 100
|
||||
|
||||
/* 层序遍历 */
|
||||
int *levelOrder(TreeNode *root, int *size) {
|
||||
/* 辅助队列 */
|
||||
|
@ -15,14 +17,14 @@ int *levelOrder(TreeNode *root, int *size) {
|
|||
TreeNode **queue;
|
||||
|
||||
/* 辅助队列 */
|
||||
queue = (TreeNode **)malloc(sizeof(TreeNode *) * MAX_NODE_SIZE);
|
||||
queue = (TreeNode **)malloc(sizeof(TreeNode *) * MAX_SIZE);
|
||||
// 队列指针
|
||||
front = 0, rear = 0;
|
||||
// 加入根节点
|
||||
queue[rear++] = root;
|
||||
// 初始化一个列表,用于保存遍历序列
|
||||
/* 辅助数组 */
|
||||
arr = (int *)malloc(sizeof(int) * MAX_NODE_SIZE);
|
||||
arr = (int *)malloc(sizeof(int) * MAX_SIZE);
|
||||
// 数组指针
|
||||
index = 0;
|
||||
while (front < rear) {
|
||||
|
@ -54,7 +56,7 @@ int main() {
|
|||
// 这里借助了一个从数组直接生成二叉树的函数
|
||||
int nums[] = {1, 2, 3, 4, 5, 6, 7};
|
||||
int size = sizeof(nums) / sizeof(int);
|
||||
TreeNode *root = arrToTree(nums, size);
|
||||
TreeNode *root = arrayToTree(nums, size);
|
||||
printf("初始化二叉树\n");
|
||||
printTree(root);
|
||||
|
||||
|
|
|
@ -6,7 +6,9 @@
|
|||
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* 辅助数组,用于存储遍历序列 */
|
||||
#define MAX_SIZE 100
|
||||
|
||||
// 辅助数组,用于存储遍历序列
|
||||
int *arr;
|
||||
|
||||
/* 前序遍历 */
|
||||
|
@ -45,13 +47,13 @@ int main() {
|
|||
// 这里借助了一个从数组直接生成二叉树的函数
|
||||
int nums[] = {1, 2, 3, 4, 5, 6, 7};
|
||||
int size = sizeof(nums) / sizeof(int);
|
||||
TreeNode *root = arrToTree(nums, size);
|
||||
TreeNode *root = arrayToTree(nums, size);
|
||||
printf("初始化二叉树\n");
|
||||
printTree(root);
|
||||
|
||||
/* 前序遍历 */
|
||||
// 初始化辅助数组
|
||||
arr = (int *)malloc(sizeof(int) * MAX_NODE_SIZE);
|
||||
arr = (int *)malloc(sizeof(int) * MAX_SIZE);
|
||||
size = 0;
|
||||
preOrder(root, &size);
|
||||
printf("前序遍历的节点打印序列 = ");
|
||||
|
|
Loading…
Reference in a new issue