diff --git a/docs/chapter_array_and_linkedlist/list.md b/docs/chapter_array_and_linkedlist/list.md index 88a4a0748..c4c6f672c 100755 --- a/docs/chapter_array_and_linkedlist/list.md +++ b/docs/chapter_array_and_linkedlist/list.md @@ -237,9 +237,9 @@ comments: true ```rust title="list.rs" /* 访问元素 */ - let num: i32 = nums[1]; // 访问索引 1 处的元素 + let num: i32 = nums[1]; // 访问索引 1 处的元素 /* 更新元素 */ - nums[1] = 0; // 将索引 1 处的元素更新为 0 + nums[1] = 0; // 将索引 1 处的元素更新为 0 ``` === "C" diff --git a/docs/chapter_backtracking/permutations_problem.md b/docs/chapter_backtracking/permutations_problem.md index 7e26f1659..673a3d35b 100644 --- a/docs/chapter_backtracking/permutations_problem.md +++ b/docs/chapter_backtracking/permutations_problem.md @@ -99,7 +99,7 @@ comments: true // 遍历所有选择 for (int i = 0; i < choices.size(); i++) { int choice = choices[i]; - // 剪枝:不允许重复选择元素 且 不允许重复选择相等元素 + // 剪枝:不允许重复选择元素 if (!selected[i]) { // 尝试:做出选择,更新状态 selected[i] = true; @@ -136,7 +136,7 @@ comments: true // 遍历所有选择 for (int i = 0; i < choices.length; i++) { int choice = choices[i]; - // 剪枝:不允许重复选择元素 且 不允许重复选择相等元素 + // 剪枝:不允许重复选择元素 if (!selected[i]) { // 尝试:做出选择,更新状态 selected[i] = true; @@ -171,7 +171,7 @@ comments: true // 遍历所有选择 for (int i = 0; i < choices.Length; i++) { int choice = choices[i]; - // 剪枝:不允许重复选择元素 且 不允许重复选择相等元素 + // 剪枝:不允许重复选择元素 if (!selected[i]) { // 尝试:做出选择,更新状态 selected[i] = true; @@ -206,7 +206,7 @@ comments: true // 遍历所有选择 for i := 0; i < len(*choices); i++ { choice := (*choices)[i] - // 剪枝:不允许重复选择元素 且 不允许重复选择相等元素 + // 剪枝:不允许重复选择元素 if !(*selected)[i] { // 尝试:做出选择,更新状态 (*selected)[i] = true @@ -242,7 +242,7 @@ comments: true } // 遍历所有选择 for (i, choice) in choices.enumerated() { - // 剪枝:不允许重复选择元素 且 不允许重复选择相等元素 + // 剪枝:不允许重复选择元素 if !selected[i] { // 尝试:做出选择,更新状态 selected[i] = true @@ -278,7 +278,7 @@ comments: true } // 遍历所有选择 choices.forEach((choice, i) => { - // 剪枝:不允许重复选择元素 且 不允许重复选择相等元素 + // 剪枝:不允许重复选择元素 if (!selected[i]) { // 尝试:做出选择,更新状态 selected[i] = true; @@ -317,7 +317,7 @@ comments: true } // 遍历所有选择 choices.forEach((choice, i) => { - // 剪枝:不允许重复选择元素 且 不允许重复选择相等元素 + // 剪枝:不允许重复选择元素 if (!selected[i]) { // 尝试:做出选择,更新状态 selected[i] = true; @@ -357,7 +357,7 @@ comments: true // 遍历所有选择 for (int i = 0; i < choices.length; i++) { int choice = choices[i]; - // 剪枝:不允许重复选择元素 且 不允许重复选择相等元素 + // 剪枝:不允许重复选择元素 if (!selected[i]) { // 尝试:做出选择,更新状态 selected[i] = true; @@ -392,7 +392,7 @@ comments: true // 遍历所有选择 for i in 0..choices.len() { let choice = choices[i]; - // 剪枝:不允许重复选择元素 且 不允许重复选择相等元素 + // 剪枝:不允许重复选择元素 if !selected[i] { // 尝试:做出选择,更新状态 selected[i] = true; @@ -418,49 +418,47 @@ comments: true ```c title="permutations_i.c" /* 回溯算法:全排列 I */ - void backtrack(vector *state, vector *choices, vector *selected, vector *res) { + void backtrack(int *state, int stateSize, int *choices, int choicesSize, bool *selected, int **res, int *resSize) { // 当状态长度等于元素数量时,记录解 - if (state->size == choices->size) { - vector *newState = newVector(); - for (int i = 0; i < state->size; i++) { - vectorPushback(newState, state->data[i], sizeof(int)); + if (stateSize == choicesSize) { + res[*resSize] = (int *)malloc(choicesSize * sizeof(int)); + for (int i = 0; i < choicesSize; i++) { + res[*resSize][i] = state[i]; } - vectorPushback(res, newState, sizeof(vector)); + (*resSize)++; 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) { + for (int i = 0; i < choicesSize; i++) { + int choice = choices[i]; + // 剪枝:不允许重复选择元素 + if (!selected[i]) { // 尝试:做出选择,更新状态 - *((bool *)selected->data[i]) = true; - vectorPushback(state, choice, sizeof(int)); + selected[i] = true; + state[stateSize] = choice; // 进行下一轮选择 - backtrack(state, choices, selected, res); + backtrack(state, stateSize + 1, choices, choicesSize, selected, res, resSize); // 回退:撤销选择,恢复到之前的状态 - *((bool *)selected->data[i]) = false; - vectorPopback(state); + selected[i] = false; } } } /* 全排列 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], sizeof(int)); + int **permutationsI(int *nums, int numsSize, int *returnSize) { + int *state = (int *)malloc(numsSize * sizeof(int)); + bool *selected = (bool *)malloc(numsSize * sizeof(bool)); + for (int i = 0; i < numsSize; i++) { + selected[i] = false; } + int **res = (int **)malloc(MAX_SIZE * sizeof(int *)); + *returnSize = 0; - vector *res = newVector(); + backtrack(state, 0, nums, numsSize, selected, res, returnSize); + + free(state); + free(selected); - // 前序遍历 - backtrack(iState, nums, bSelected, res); return res; } ``` @@ -888,9 +886,52 @@ comments: true === "C" ```c title="permutations_ii.c" - [class]{}-[func]{backtrack} + /* 回溯算法:全排列 II */ + void backtrack(int *state, int stateSize, int *choices, int choicesSize, bool *selected, int **res, int *resSize) { + // 当状态长度等于元素数量时,记录解 + if (stateSize == choicesSize) { + res[*resSize] = (int *)malloc(choicesSize * sizeof(int)); + for (int i = 0; i < choicesSize; i++) { + res[*resSize][i] = state[i]; + } + (*resSize)++; + return; + } + // 遍历所有选择 + bool duplicated[MAX_SIZE] = {false}; + for (int i = 0; i < choicesSize; i++) { + int choice = choices[i]; + // 剪枝:不允许重复选择元素 且 不允许重复选择相等元素 + if (!selected[i] && !duplicated[choice]) { + // 尝试:做出选择,更新状态 + duplicated[choice] = true; // 记录选择过的元素值 + selected[i] = true; + state[stateSize] = choice; + // 进行下一轮选择 + backtrack(state, stateSize + 1, choices, choicesSize, selected, res, resSize); + // 回退:撤销选择,恢复到之前的状态 + selected[i] = false; + } + } + } - [class]{}-[func]{permutationsII} + /* 全排列 II */ + int **permutationsII(int *nums, int numsSize, int *returnSize) { + int *state = (int *)malloc(numsSize * sizeof(int)); + bool *selected = (bool *)malloc(numsSize * sizeof(bool)); + for (int i = 0; i < numsSize; i++) { + selected[i] = false; + } + int **res = (int **)malloc(MAX_SIZE * sizeof(int *)); + *returnSize = 0; + + backtrack(state, 0, nums, numsSize, selected, res, returnSize); + + free(state); + free(selected); + + return res; + } ``` === "Zig" 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 dedbfa0c9..87777e4fe 100644 --- a/docs/chapter_divide_and_conquer/build_binary_tree_problem.md +++ b/docs/chapter_divide_and_conquer/build_binary_tree_problem.md @@ -404,9 +404,27 @@ comments: true === "C" ```c title="build_tree.c" - [class]{}-[func]{dfs} + /* 构建二叉树 */ + fn build_tree(preorder: &[i32], inorder: &[i32]) -> Option>> { + // 初始化哈希表,存储 inorder 元素到索引的映射 + let mut inorder_map: HashMap = HashMap::new(); + for i in 0..inorder.len() { + inorder_map.insert(inorder[i], i as i32); + } + let root = dfs(preorder, &inorder_map, 0, 0, inorder.len() as i32 - 1); + root + } - [class]{}-[func]{buildTree} + /* 构建二叉树 */ + fn build_tree(preorder: &[i32], inorder: &[i32]) -> Option>> { + // 初始化哈希表,存储 inorder 元素到索引的映射 + let mut inorder_map: HashMap = HashMap::new(); + for i in 0..inorder.len() { + inorder_map.insert(inorder[i], i as i32); + } + let root = dfs(preorder, &inorder_map, 0, 0, inorder.len() as i32 - 1); + root + } ``` === "Zig" diff --git a/docs/chapter_hashing/hash_algorithm.md b/docs/chapter_hashing/hash_algorithm.md index 9496bc695..c858c569d 100644 --- a/docs/chapter_hashing/hash_algorithm.md +++ b/docs/chapter_hashing/hash_algorithm.md @@ -743,7 +743,7 @@ $$ === "Go" ```go title="built_in_hash.go" - + // Go 未提供内置 hash code 函数 ``` === "Swift"