From a42c1d62b97f27337a13a858a5b851e34065f6d3 Mon Sep 17 00:00:00 2001 From: krahets Date: Fri, 27 Oct 2023 20:41:59 +0800 Subject: [PATCH] build --- docs/chapter_array_and_linkedlist/list.md | 6 +-- docs/chapter_backtracking/n_queens_problem.md | 14 +++---- .../build_binary_tree_problem.md | 2 +- docs/chapter_sorting/merge_sort.md | 41 ++++++++++--------- docs/chapter_tree/binary_tree_traversal.md | 4 +- 5 files changed, 34 insertions(+), 33 deletions(-) diff --git a/docs/chapter_array_and_linkedlist/list.md b/docs/chapter_array_and_linkedlist/list.md index 275f3f74f..886c9564d 100755 --- a/docs/chapter_array_and_linkedlist/list.md +++ b/docs/chapter_array_and_linkedlist/list.md @@ -26,9 +26,9 @@ comments: true ```python title="list.py" # 初始化列表 # 无初始值 - nums1: nums[int] = [] + nums1: list[int] = [] # 有初始值 - nums: nums[int] = [1, 3, 2, 5, 4] + nums: list[int] = [1, 3, 2, 5, 4] ``` === "C++" @@ -681,7 +681,7 @@ comments: true ```python title="list.py" # 拼接两个列表 - nums1: nums[int] = [6, 8, 7, 10, 9] + nums1: list[int] = [6, 8, 7, 10, 9] nums += nums1 # 将列表 nums1 拼接到 nums 之后 ``` diff --git a/docs/chapter_backtracking/n_queens_problem.md b/docs/chapter_backtracking/n_queens_problem.md index 3dd9a0bd0..1c0e81848 100644 --- a/docs/chapter_backtracking/n_queens_problem.md +++ b/docs/chapter_backtracking/n_queens_problem.md @@ -602,8 +602,8 @@ comments: true ```c title="n_queens.c" /* 回溯算法: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); @@ -635,7 +635,7 @@ comments: true /* 求解 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) { @@ -643,11 +643,11 @@ comments: true } 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; diff --git a/docs/chapter_divide_and_conquer/build_binary_tree_problem.md b/docs/chapter_divide_and_conquer/build_binary_tree_problem.md index 726614e45..f1e6be241 100644 --- a/docs/chapter_divide_and_conquer/build_binary_tree_problem.md +++ b/docs/chapter_divide_and_conquer/build_binary_tree_problem.md @@ -427,7 +427,7 @@ comments: true /* 构建二叉树 */ 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; } diff --git a/docs/chapter_sorting/merge_sort.md b/docs/chapter_sorting/merge_sort.md index 8a5ee28da..cf82cb6eb 100755 --- a/docs/chapter_sorting/merge_sort.md +++ b/docs/chapter_sorting/merge_sort.md @@ -437,28 +437,29 @@ comments: true ```dart title="merge_sort.dart" /* 合并左子数组和右子数组 */ - // 左子数组区间 [left, mid] - // 右子数组区间 [mid + 1, right] void merge(List nums, int left, int mid, int right) { - // 初始化辅助数组 - List tmp = nums.sublist(left, right + 1); - // 左子数组的起始索引和结束索引 - int leftStart = left - left, leftEnd = mid - left; - // 右子数组的起始索引和结束索引 - int rightStart = mid + 1 - left, rightEnd = right - left; - // i, j 分别指向左子数组、右子数组的首元素 - int i = leftStart, j = rightStart; - // 通过覆盖原数组 nums 来合并左子数组和右子数组 - for (int k = left; k <= right; k++) { - // 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++ - if (i > leftEnd) - nums[k] = tmp[j++]; - // 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++ - else if (j > rightEnd || tmp[i] <= tmp[j]) - nums[k] = tmp[i++]; - // 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ + // 左子数组区间 [left, mid], 右子数组区间 [mid+1, right] + // 创建一个临时数组 tmp ,用于存放合并后的结果 + List tmp = List.filled(right - left + 1, 0); + // 初始化左子数组和右子数组的起始索引 + int i = left, j = mid + 1, k = 0; + // 当左右子数组都还有元素时,比较并将较小的元素复制到临时数组中 + while (i <= mid && j <= right) { + if (nums[i] <= nums[j]) + tmp[k++] = nums[i++]; else - nums[k] = tmp[j++]; + tmp[k++] = nums[j++]; + } + // 将左子数组和右子数组的剩余元素复制到临时数组中 + while (i <= mid) { + tmp[k++] = nums[i++]; + } + while (j <= right) { + tmp[k++] = nums[j++]; + } + // 将临时数组 tmp 中的元素复制回原数组 nums 的对应区间 + for (k = 0; k < tmp.length; k++) { + nums[left + k] = tmp[k]; } } diff --git a/docs/chapter_tree/binary_tree_traversal.md b/docs/chapter_tree/binary_tree_traversal.md index c6a6fd1ca..ce4aa681e 100755 --- a/docs/chapter_tree/binary_tree_traversal.md +++ b/docs/chapter_tree/binary_tree_traversal.md @@ -258,14 +258,14 @@ comments: true 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) {