From f71b2a40daf6d48be3e709c6a67d847d4249d692 Mon Sep 17 00:00:00 2001 From: krahets Date: Sat, 9 Sep 2023 00:51:00 +0800 Subject: [PATCH] fix preorder_traversal_iii_compact code --- .../preorder_traversal_iii_template.c | 2 +- codes/c/chapter_hashing/hash_map_chaining.c | 26 +++++++++---------- .../preorder_traversal_iii_compact.cpp | 2 -- .../preorder_traversal_iii_compact.cs | 2 -- .../preorder_traversal_iii_compact.dart | 2 -- .../preorder_traversal_iii_compact.go | 2 -- .../preorder_traversal_iii_compact.java | 2 -- .../preorder_traversal_iii_compact.js | 2 -- .../preorder_traversal_iii_compact.py | 2 -- .../preorder_traversal_iii_compact.rs | 2 -- .../preorder_traversal_iii_compact.swift | 2 -- .../preorder_traversal_iii_compact.ts | 2 -- docs/chapter_array_and_linkedlist/index.md | 2 +- .../backtracking_algorithm.md | 4 +-- 14 files changed, 17 insertions(+), 37 deletions(-) diff --git a/codes/c/chapter_backtracking/preorder_traversal_iii_template.c b/codes/c/chapter_backtracking/preorder_traversal_iii_template.c index eb82b8db8..11ff691f2 100644 --- a/codes/c/chapter_backtracking/preorder_traversal_iii_template.c +++ b/codes/c/chapter_backtracking/preorder_traversal_iii_template.c @@ -35,7 +35,7 @@ void undoChoice(vector *state, TreeNode *choice) { vectorPopback(state); } -/* 前序遍历:例题三 */ +/* 回溯算法:例题三 */ void backtrack(vector *state, vector *choices, vector *res) { // 检查是否为解 if (isSolution(state)) { diff --git a/codes/c/chapter_hashing/hash_map_chaining.c b/codes/c/chapter_hashing/hash_map_chaining.c index e93f0ef9b..de8cb11fb 100644 --- a/codes/c/chapter_hashing/hash_map_chaining.c +++ b/codes/c/chapter_hashing/hash_map_chaining.c @@ -30,16 +30,16 @@ struct hashMapChaining { Pair *buckets; // 桶数组 }; -typedef struct hashMapChaining HashMapChaining; +typedef struct hashMapChaining hashMapChaining; // 函数声明 -void extend(HashMapChaining *hashmap); +void extend(hashMapChaining *hashmap); /* 初始化桶数组 */ -HashMapChaining *newHashMapChaining() { +hashMapChaining *newHashMapChaining() { // 为哈希表分配空间 int tableSize = 4; - HashMapChaining *hashmap = (HashMapChaining *)malloc(sizeof(HashMapChaining)); + hashMapChaining *hashmap = (hashMapChaining *)malloc(sizeof(hashMapChaining)); // 初始化数组 hashmap->buckets = (Pair *)malloc(sizeof(Pair) * tableSize); @@ -54,7 +54,7 @@ HashMapChaining *newHashMapChaining() { } /* 销毁哈希表 */ -void delHashMapChaining(HashMapChaining *hashmap) { +void delHashMapChaining(hashMapChaining *hashmap) { for (int i = 0; i < hashmap->capacity; i++) { Pair *pair = &hashmap->buckets[i]; Node *node = pair->node; @@ -70,17 +70,17 @@ void delHashMapChaining(HashMapChaining *hashmap) { } /* 哈希函数 */ -int hashFunc(HashMapChaining *hashmap, const int key) { +int hashFunc(hashMapChaining *hashmap, const int key) { return key % hashmap->capacity; } /* 负载因子 */ -double loadFactor(HashMapChaining *hashmap) { +double loadFactor(hashMapChaining *hashmap) { return (double)hashmap->size / (double)hashmap->capacity; } /* 查询操作 */ -const char *get(HashMapChaining *hashmap, const int key) { +const char *get(hashMapChaining *hashmap, const int key) { int index = hashFunc(hashmap, key); Pair *pair = &hashmap->buckets[index]; Node *node = pair->node; @@ -93,7 +93,7 @@ const char *get(HashMapChaining *hashmap, const int key) { } /* 添加操作 */ -void put(HashMapChaining *hashmap, const int key, char *val) { +void put(hashMapChaining *hashmap, const int key, char *val) { if (loadFactor(hashmap) > hashmap->loadThres) { extend(hashmap); } @@ -134,7 +134,7 @@ void put(HashMapChaining *hashmap, const int key, char *val) { } /* 删除操作 */ -void removeItem(HashMapChaining *hashmap, int key) { +void removeItem(hashMapChaining *hashmap, int key) { int index = hashFunc(hashmap, key); Pair *pair = &hashmap->buckets[index]; Node *node = pair->node; @@ -161,7 +161,7 @@ void removeItem(HashMapChaining *hashmap, int key) { } /* 扩容哈希表 */ -void extend(HashMapChaining *hashmap) { +void extend(hashMapChaining *hashmap) { // 暂存原哈希表 Pair *oldBuckets = hashmap->buckets; int oldCapacity = hashmap->capacity; @@ -195,7 +195,7 @@ void extend(HashMapChaining *hashmap) { } /* 打印哈希表 */ -void print(HashMapChaining *hashmap) { +void print(hashMapChaining *hashmap) { for (int i = 0; i < hashmap->capacity; i++) { printf("["); Pair *pair = &hashmap->buckets[i]; @@ -214,7 +214,7 @@ void print(HashMapChaining *hashmap) { /* Driver Code */ int main() { /* 初始化哈希表 */ - HashMapChaining *map = newHashMapChaining(); + hashMapChaining *map = newHashMapChaining(); /* 添加操作 */ // 在哈希表中添加键值对 (key, value) diff --git a/codes/cpp/chapter_backtracking/preorder_traversal_iii_compact.cpp b/codes/cpp/chapter_backtracking/preorder_traversal_iii_compact.cpp index 84207294e..420f44924 100644 --- a/codes/cpp/chapter_backtracking/preorder_traversal_iii_compact.cpp +++ b/codes/cpp/chapter_backtracking/preorder_traversal_iii_compact.cpp @@ -20,8 +20,6 @@ void preOrder(TreeNode *root) { if (root->val == 7) { // 记录解 res.push_back(path); - path.pop_back(); - return; } preOrder(root->left); preOrder(root->right); diff --git a/codes/csharp/chapter_backtracking/preorder_traversal_iii_compact.cs b/codes/csharp/chapter_backtracking/preorder_traversal_iii_compact.cs index 975b861d2..f12889a1d 100644 --- a/codes/csharp/chapter_backtracking/preorder_traversal_iii_compact.cs +++ b/codes/csharp/chapter_backtracking/preorder_traversal_iii_compact.cs @@ -21,8 +21,6 @@ public class preorder_traversal_iii_compact { if (root.val == 7) { // 记录解 res.Add(new List(path)); - path.RemoveAt(path.Count - 1); - return; } preOrder(root.left); preOrder(root.right); diff --git a/codes/dart/chapter_backtracking/preorder_traversal_iii_compact.dart b/codes/dart/chapter_backtracking/preorder_traversal_iii_compact.dart index 93137fc91..93cc684c9 100644 --- a/codes/dart/chapter_backtracking/preorder_traversal_iii_compact.dart +++ b/codes/dart/chapter_backtracking/preorder_traversal_iii_compact.dart @@ -22,8 +22,6 @@ void preOrder( if (root.val == 7) { // 记录解 res.add(List.from(path)); - path.removeLast(); - return; } preOrder(root.left, path, res); preOrder(root.right, path, res); diff --git a/codes/go/chapter_backtracking/preorder_traversal_iii_compact.go b/codes/go/chapter_backtracking/preorder_traversal_iii_compact.go index 700371cda..a94c3a937 100644 --- a/codes/go/chapter_backtracking/preorder_traversal_iii_compact.go +++ b/codes/go/chapter_backtracking/preorder_traversal_iii_compact.go @@ -19,8 +19,6 @@ func preOrderIII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) { if root.Val.(int) == 7 { // 记录解 *res = append(*res, *path) - *path = (*path)[:len(*path)-1] - return } preOrderIII(root.Left, res, path) preOrderIII(root.Right, res, path) diff --git a/codes/java/chapter_backtracking/preorder_traversal_iii_compact.java b/codes/java/chapter_backtracking/preorder_traversal_iii_compact.java index 9ab3bb8d9..a2b6705f5 100644 --- a/codes/java/chapter_backtracking/preorder_traversal_iii_compact.java +++ b/codes/java/chapter_backtracking/preorder_traversal_iii_compact.java @@ -24,8 +24,6 @@ public class preorder_traversal_iii_compact { if (root.val == 7) { // 记录解 res.add(new ArrayList<>(path)); - path.remove(path.size() - 1); - return; } preOrder(root.left); preOrder(root.right); diff --git a/codes/javascript/chapter_backtracking/preorder_traversal_iii_compact.js b/codes/javascript/chapter_backtracking/preorder_traversal_iii_compact.js index 2c2cbb93d..5b942df67 100644 --- a/codes/javascript/chapter_backtracking/preorder_traversal_iii_compact.js +++ b/codes/javascript/chapter_backtracking/preorder_traversal_iii_compact.js @@ -18,8 +18,6 @@ function preOrder(root, path, res) { if (root.val === 7) { // 记录解 res.push([...path]); - path.pop(); - return; } preOrder(root.left, path, res); preOrder(root.right, path, res); diff --git a/codes/python/chapter_backtracking/preorder_traversal_iii_compact.py b/codes/python/chapter_backtracking/preorder_traversal_iii_compact.py index f5af42fc4..dac756c79 100644 --- a/codes/python/chapter_backtracking/preorder_traversal_iii_compact.py +++ b/codes/python/chapter_backtracking/preorder_traversal_iii_compact.py @@ -20,8 +20,6 @@ def pre_order(root: TreeNode): if root.val == 7: # 记录解 res.append(list(path)) - path.pop() - return pre_order(root.left) pre_order(root.right) # 回退 diff --git a/codes/rust/chapter_backtracking/preorder_traversal_iii_compact.rs b/codes/rust/chapter_backtracking/preorder_traversal_iii_compact.rs index f88662f46..62d29f237 100644 --- a/codes/rust/chapter_backtracking/preorder_traversal_iii_compact.rs +++ b/codes/rust/chapter_backtracking/preorder_traversal_iii_compact.rs @@ -21,8 +21,6 @@ fn pre_order(res: &mut Vec>>>, path: &mut Vec