Skip to content

Commit

Permalink
A few improvements to the C code.
Browse files Browse the repository at this point in the history
  • Loading branch information
krahets committed Oct 26, 2023
1 parent 0e10274 commit bd8cda3
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 18 deletions.
17 changes: 8 additions & 9 deletions codes/c/chapter_backtracking/n_queens.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -43,19 +42,19 @@ 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) {
state[i][j] = '#';
}
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;
Expand Down
4 changes: 2 additions & 2 deletions codes/c/chapter_divide_and_conquer/build_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion codes/c/chapter_sorting/selection_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
8 changes: 5 additions & 3 deletions codes/c/chapter_tree/binary_tree_bfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include "../utils/common.h"

#define MAX_SIZE 100

/* 层序遍历 */
int *levelOrder(TreeNode *root, int *size) {
/* 辅助队列 */
Expand All @@ -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) {
Expand Down Expand Up @@ -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);

Expand Down
8 changes: 5 additions & 3 deletions codes/c/chapter_tree/binary_tree_dfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

#include "../utils/common.h"

/* 辅助数组,用于存储遍历序列 */
#define MAX_SIZE 100

// 辅助数组,用于存储遍历序列
int *arr;

/* 前序遍历 */
Expand Down Expand Up @@ -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("前序遍历的节点打印序列 = ");
Expand Down

0 comments on commit bd8cda3

Please sign in to comment.