diff --git a/docs/chapter_searching/replace_linear_by_hashing.assets/two_sum_brute_force.png b/docs/chapter_searching/replace_linear_by_hashing.assets/two_sum_brute_force.png index 33fca976a..a9db1579d 100644 Binary files a/docs/chapter_searching/replace_linear_by_hashing.assets/two_sum_brute_force.png and b/docs/chapter_searching/replace_linear_by_hashing.assets/two_sum_brute_force.png differ diff --git a/docs/index.html b/docs/index.html index f97ab5cba..bfb257f9b 100644 --- a/docs/index.html +++ b/docs/index.html @@ -353,4 +353,4 @@ - + \ No newline at end of file diff --git a/en/docs/chapter_searching/replace_linear_by_hashing.assets/two_sum_brute_force.png b/en/docs/chapter_searching/replace_linear_by_hashing.assets/two_sum_brute_force.png index 6e942957a..8481ea4ec 100644 Binary files a/en/docs/chapter_searching/replace_linear_by_hashing.assets/two_sum_brute_force.png and b/en/docs/chapter_searching/replace_linear_by_hashing.assets/two_sum_brute_force.png differ diff --git a/en/docs/index.html b/en/docs/index.html index e6910529d..01a95b5a5 100644 --- a/en/docs/index.html +++ b/en/docs/index.html @@ -420,4 +420,4 @@ - + \ No newline at end of file diff --git a/zh-hant/codes/c/chapter_searching/two_sum.c b/zh-hant/codes/c/chapter_searching/two_sum.c index 30f32dd9e..b753b354a 100644 --- a/zh-hant/codes/c/chapter_searching/two_sum.c +++ b/zh-hant/codes/c/chapter_searching/two_sum.c @@ -37,12 +37,12 @@ HashTable *find(HashTable *h, int key) { } /* 雜湊表元素插入 */ -void insert(HashTable *h, int key, int val) { - HashTable *t = find(h, key); +void insert(HashTable **h, int key, int val) { + HashTable *t = find(*h, key); if (t == NULL) { HashTable *tmp = malloc(sizeof(HashTable)); tmp->key = key, tmp->val = val; - HASH_ADD_INT(h, key, tmp); + HASH_ADD_INT(*h, key, tmp); } else { t->val = val; } @@ -59,7 +59,7 @@ int *twoSumHashTable(int *nums, int numsSize, int target, int *returnSize) { *returnSize = 2; return res; } - insert(hashtable, nums[i], i); + insert(&hashtable, nums[i], i); } *returnSize = 0; return NULL; @@ -83,4 +83,4 @@ int main() { printArray(res, returnSize); return 0; -} +} \ No newline at end of file diff --git a/zh-hant/codes/c/chapter_sorting/counting_sort.c b/zh-hant/codes/c/chapter_sorting/counting_sort.c index 3c887a981..ba1759fab 100644 --- a/zh-hant/codes/c/chapter_sorting/counting_sort.c +++ b/zh-hant/codes/c/chapter_sorting/counting_sort.c @@ -65,6 +65,7 @@ void countingSort(int nums[], int size) { // 使用結果陣列 res 覆蓋原陣列 nums memcpy(nums, res, size * sizeof(int)); // 5. 釋放記憶體 + free(res); free(counter); } diff --git a/zh-hant/codes/c/chapter_sorting/radix_sort.c b/zh-hant/codes/c/chapter_sorting/radix_sort.c index 6598dd0e7..cdb7effb9 100644 --- a/zh-hant/codes/c/chapter_sorting/radix_sort.c +++ b/zh-hant/codes/c/chapter_sorting/radix_sort.c @@ -16,6 +16,7 @@ int digit(int num, int exp) { void countingSortDigit(int nums[], int size, int exp) { // 十進位制的位範圍為 0~9 ,因此需要長度為 10 的桶陣列 int *counter = (int *)malloc((sizeof(int) * 10)); + memset(counter, 0, sizeof(int) * 10); // 初始化為 0 以支持後續記憶體釋放 // 統計 0~9 各數字的出現次數 for (int i = 0; i < size; i++) { // 獲取 nums[i] 第 k 位,記為 d @@ -39,13 +40,16 @@ void countingSortDigit(int nums[], int size, int exp) { for (int i = 0; i < size; i++) { nums[i] = res[i]; } + // 釋放記憶體 + free(res); + free(counter); } /* 基數排序 */ void radixSort(int nums[], int size) { // 獲取陣列的最大元素,用於判斷最大位數 int max = INT32_MIN; - for (size_t i = 0; i < size - 1; i++) { + for (int i = 0; i < size; i++) { if (nums[i] > max) { max = nums[i]; } diff --git a/zh-hant/codes/csharp/chapter_dynamic_programming/min_path_sum.cs b/zh-hant/codes/csharp/chapter_dynamic_programming/min_path_sum.cs index c25f149d8..f3f77e09c 100644 --- a/zh-hant/codes/csharp/chapter_dynamic_programming/min_path_sum.cs +++ b/zh-hant/codes/csharp/chapter_dynamic_programming/min_path_sum.cs @@ -105,7 +105,7 @@ public class min_path_sum { // 暴力搜尋 int res = MinPathSumDFS(grid, n - 1, m - 1); - Console.WriteLine("從左上角到右下角的做小路徑和為 " + res); + Console.WriteLine("從左上角到右下角的最小路徑和為 " + res); // 記憶化搜尋 int[][] mem = new int[n][]; @@ -114,14 +114,14 @@ public class min_path_sum { Array.Fill(mem[i], -1); } res = MinPathSumDFSMem(grid, mem, n - 1, m - 1); - Console.WriteLine("從左上角到右下角的做小路徑和為 " + res); + Console.WriteLine("從左上角到右下角的最小路徑和為 " + res); // 動態規劃 res = MinPathSumDP(grid); - Console.WriteLine("從左上角到右下角的做小路徑和為 " + res); + Console.WriteLine("從左上角到右下角的最小路徑和為 " + res); // 空間最佳化後的動態規劃 res = MinPathSumDPComp(grid); - Console.WriteLine("從左上角到右下角的做小路徑和為 " + res); + Console.WriteLine("從左上角到右下角的最小路徑和為 " + res); } } diff --git a/zh-hant/codes/csharp/chapter_heap/heap.cs b/zh-hant/codes/csharp/chapter_heap/heap.cs index ac8b34d54..71289029e 100644 --- a/zh-hant/codes/csharp/chapter_heap/heap.cs +++ b/zh-hant/codes/csharp/chapter_heap/heap.cs @@ -24,8 +24,8 @@ public class heap { /* 初始化堆積 */ // 初始化小頂堆積 PriorityQueue minHeap = new(); - // 初始化大頂堆積(使用 lambda 表示式修改 Comparator 即可) - PriorityQueue maxHeap = new(Comparer.Create((x, y) => y - x)); + // 初始化大頂堆積(使用 lambda 表示式修改 Comparer 即可) + PriorityQueue maxHeap = new(Comparer.Create((x, y) => y.CompareTo(x))); Console.WriteLine("以下測試樣例為大頂堆積"); /* 元素入堆積 */ diff --git a/zh-hant/codes/dart/chapter_dynamic_programming/min_path_sum.dart b/zh-hant/codes/dart/chapter_dynamic_programming/min_path_sum.dart index 4cf15a754..6e897cb68 100644 --- a/zh-hant/codes/dart/chapter_dynamic_programming/min_path_sum.dart +++ b/zh-hant/codes/dart/chapter_dynamic_programming/min_path_sum.dart @@ -103,18 +103,18 @@ void main() { // 暴力搜尋 int res = minPathSumDFS(grid, n - 1, m - 1); - print("從左上角到右下角的做小路徑和為 $res"); + print("從左上角到右下角的最小路徑和為 $res"); // 記憶化搜尋 List> mem = List.generate(n, (i) => List.filled(m, -1)); res = minPathSumDFSMem(grid, mem, n - 1, m - 1); - print("從左上角到右下角的做小路徑和為 $res"); + print("從左上角到右下角的最小路徑和為 $res"); // 動態規劃 res = minPathSumDP(grid); - print("從左上角到右下角的做小路徑和為 $res"); + print("從左上角到右下角的最小路徑和為 $res"); // 空間最佳化後的動態規劃 res = minPathSumDPComp(grid); - print("從左上角到右下角的做小路徑和為 $res"); + print("從左上角到右下角的最小路徑和為 $res"); } diff --git a/zh-hant/codes/go/chapter_dynamic_programming/min_path_sum_test.go b/zh-hant/codes/go/chapter_dynamic_programming/min_path_sum_test.go index 9a57c6c31..e8e8ce51a 100644 --- a/zh-hant/codes/go/chapter_dynamic_programming/min_path_sum_test.go +++ b/zh-hant/codes/go/chapter_dynamic_programming/min_path_sum_test.go @@ -20,7 +20,7 @@ func TestMinPathSum(t *testing.T) { // 暴力搜尋 res := minPathSumDFS(grid, n-1, m-1) - fmt.Printf("從左上角到右下角的做小路徑和為 %d\n", res) + fmt.Printf("從左上角到右下角的最小路徑和為 %d\n", res) // 記憶化搜尋 mem := make([][]int, n) @@ -31,13 +31,13 @@ func TestMinPathSum(t *testing.T) { } } res = minPathSumDFSMem(grid, mem, n-1, m-1) - fmt.Printf("從左上角到右下角的做小路徑和為 %d\n", res) + fmt.Printf("從左上角到右下角的最小路徑和為 %d\n", res) // 動態規劃 res = minPathSumDP(grid) - fmt.Printf("從左上角到右下角的做小路徑和為 %d\n", res) + fmt.Printf("從左上角到右下角的最小路徑和為 %d\n", res) // 空間最佳化後的動態規劃 res = minPathSumDPComp(grid) - fmt.Printf("從左上角到右下角的做小路徑和為 %d\n", res) + fmt.Printf("從左上角到右下角的最小路徑和為 %d\n", res) } diff --git a/zh-hant/codes/python/chapter_dynamic_programming/min_path_sum.py b/zh-hant/codes/python/chapter_dynamic_programming/min_path_sum.py index 97d20e9d8..f202abcae 100644 --- a/zh-hant/codes/python/chapter_dynamic_programming/min_path_sum.py +++ b/zh-hant/codes/python/chapter_dynamic_programming/min_path_sum.py @@ -88,17 +88,17 @@ if __name__ == "__main__": # 暴力搜尋 res = min_path_sum_dfs(grid, n - 1, m - 1) - print(f"從左上角到右下角的做小路徑和為 {res}") + print(f"從左上角到右下角的最小路徑和為 {res}") # 記憶化搜尋 mem = [[-1] * m for _ in range(n)] res = min_path_sum_dfs_mem(grid, mem, n - 1, m - 1) - print(f"從左上角到右下角的做小路徑和為 {res}") + print(f"從左上角到右下角的最小路徑和為 {res}") # 動態規劃 res = min_path_sum_dp(grid) - print(f"從左上角到右下角的做小路徑和為 {res}") + print(f"從左上角到右下角的最小路徑和為 {res}") # 空間最佳化後的動態規劃 res = min_path_sum_dp_comp(grid) - print(f"從左上角到右下角的做小路徑和為 {res}") + print(f"從左上角到右下角的最小路徑和為 {res}") diff --git a/zh-hant/codes/ruby/chapter_dynamic_programming/min_path_sum.rb b/zh-hant/codes/ruby/chapter_dynamic_programming/min_path_sum.rb index 32591613e..141d1b92d 100644 --- a/zh-hant/codes/ruby/chapter_dynamic_programming/min_path_sum.rb +++ b/zh-hant/codes/ruby/chapter_dynamic_programming/min_path_sum.rb @@ -76,18 +76,18 @@ if __FILE__ == $0 # 暴力搜尋 res = min_path_sum_dfs(grid, n - 1, m - 1) - puts "從左上角到右下角的做小路徑和為 #{res}" + puts "從左上角到右下角的最小路徑和為 #{res}" # 記憶化搜尋 mem = Array.new(n) { Array.new(m, - 1) } res = min_path_sum_dfs_mem(grid, mem, n - 1, m -1) - puts "從左上角到右下角的做小路徑和為 #{res}" + puts "從左上角到右下角的最小路徑和為 #{res}" # 動態規劃 res = min_path_sum_dp(grid) - puts "從左上角到右下角的做小路徑和為 #{res}" + puts "從左上角到右下角的最小路徑和為 #{res}" # 空間最佳化後的動態規劃 res = min_path_sum_dp_comp(grid) - puts "從左上角到右下角的做小路徑和為 #{res}" + puts "從左上角到右下角的最小路徑和為 #{res}" end diff --git a/zh-hant/codes/rust/chapter_array_and_linkedlist/array.rs b/zh-hant/codes/rust/chapter_array_and_linkedlist/array.rs index f276b6d74..1184dcfff 100644 --- a/zh-hant/codes/rust/chapter_array_and_linkedlist/array.rs +++ b/zh-hant/codes/rust/chapter_array_and_linkedlist/array.rs @@ -4,8 +4,7 @@ * Author: xBLACICEx (xBLACKICEx@outlook.com), codingonion (coderonion@gmail.com) */ -include!("../include/include.rs"); - +use hello_algo_rust::include::print_util; use rand::Rng; /* 隨機訪問元素 */ diff --git a/zh-hant/codes/rust/chapter_array_and_linkedlist/linked_list.rs b/zh-hant/codes/rust/chapter_array_and_linkedlist/linked_list.rs index d9020ed8c..916cb5121 100644 --- a/zh-hant/codes/rust/chapter_array_and_linkedlist/linked_list.rs +++ b/zh-hant/codes/rust/chapter_array_and_linkedlist/linked_list.rs @@ -4,9 +4,7 @@ * Author: codingonion (coderonion@gmail.com) */ -include!("../include/include.rs"); - -use list_node::ListNode; +use hello_algo_rust::include::{print_util, ListNode}; use std::cell::RefCell; use std::rc::Rc; diff --git a/zh-hant/codes/rust/chapter_array_and_linkedlist/list.rs b/zh-hant/codes/rust/chapter_array_and_linkedlist/list.rs index dd0d8706b..e7ab8b05a 100644 --- a/zh-hant/codes/rust/chapter_array_and_linkedlist/list.rs +++ b/zh-hant/codes/rust/chapter_array_and_linkedlist/list.rs @@ -3,8 +3,7 @@ * Created Time: 2023-01-18 * Author: xBLACICEx (xBLACKICEx@outlook.com), codingonion (coderonion@gmail.com) */ - -include!("../include/include.rs"); +use hello_algo_rust::include::print_util; /* Driver Code */ fn main() { diff --git a/zh-hant/codes/rust/chapter_array_and_linkedlist/my_list.rs b/zh-hant/codes/rust/chapter_array_and_linkedlist/my_list.rs index 04a8a6612..8c6991d64 100644 --- a/zh-hant/codes/rust/chapter_array_and_linkedlist/my_list.rs +++ b/zh-hant/codes/rust/chapter_array_and_linkedlist/my_list.rs @@ -4,7 +4,7 @@ * Author: codingonion (coderonion@gmail.com) */ -include!("../include/include.rs"); +use hello_algo_rust::include::print_util; /* 串列類別 */ #[allow(dead_code)] diff --git a/zh-hant/codes/rust/chapter_backtracking/preorder_traversal_i_compact.rs b/zh-hant/codes/rust/chapter_backtracking/preorder_traversal_i_compact.rs index 7e94e8c53..cae60f54d 100644 --- a/zh-hant/codes/rust/chapter_backtracking/preorder_traversal_i_compact.rs +++ b/zh-hant/codes/rust/chapter_backtracking/preorder_traversal_i_compact.rs @@ -4,10 +4,8 @@ * Author: codingonion (coderonion@gmail.com) */ -include!("../include/include.rs"); - +use hello_algo_rust::include::{print_util, vec_to_tree, TreeNode}; use std::{cell::RefCell, rc::Rc}; -use tree_node::{vec_to_tree, TreeNode}; /* 前序走訪:例題一 */ fn pre_order(res: &mut Vec>>, root: Option<&Rc>>) { diff --git a/zh-hant/codes/rust/chapter_backtracking/preorder_traversal_ii_compact.rs b/zh-hant/codes/rust/chapter_backtracking/preorder_traversal_ii_compact.rs index d709738a0..b627ebae2 100644 --- a/zh-hant/codes/rust/chapter_backtracking/preorder_traversal_ii_compact.rs +++ b/zh-hant/codes/rust/chapter_backtracking/preorder_traversal_ii_compact.rs @@ -4,10 +4,8 @@ * Author: codingonion (coderonion@gmail.com) */ -include!("../include/include.rs"); - +use hello_algo_rust::include::{print_util, vec_to_tree, TreeNode}; use std::{cell::RefCell, rc::Rc}; -use tree_node::{vec_to_tree, TreeNode}; /* 前序走訪:例題二 */ fn pre_order( diff --git a/zh-hant/codes/rust/chapter_backtracking/preorder_traversal_iii_compact.rs b/zh-hant/codes/rust/chapter_backtracking/preorder_traversal_iii_compact.rs index f61b86288..9e449bdbb 100644 --- a/zh-hant/codes/rust/chapter_backtracking/preorder_traversal_iii_compact.rs +++ b/zh-hant/codes/rust/chapter_backtracking/preorder_traversal_iii_compact.rs @@ -4,10 +4,8 @@ * Author: codingonion (coderonion@gmail.com) */ -include!("../include/include.rs"); - +use hello_algo_rust::include::{print_util, vec_to_tree, TreeNode}; use std::{cell::RefCell, rc::Rc}; -use tree_node::{vec_to_tree, TreeNode}; /* 前序走訪:例題三 */ fn pre_order( diff --git a/zh-hant/codes/rust/chapter_backtracking/preorder_traversal_iii_template.rs b/zh-hant/codes/rust/chapter_backtracking/preorder_traversal_iii_template.rs index 8b7deff79..7a1663b7d 100644 --- a/zh-hant/codes/rust/chapter_backtracking/preorder_traversal_iii_template.rs +++ b/zh-hant/codes/rust/chapter_backtracking/preorder_traversal_iii_template.rs @@ -4,10 +4,8 @@ * Author: codingonion (coderonion@gmail.com) */ -include!("../include/include.rs"); - +use hello_algo_rust::include::{print_util, vec_to_tree, TreeNode}; use std::{cell::RefCell, rc::Rc}; -use tree_node::{vec_to_tree, TreeNode}; /* 判斷當前狀態是否為解 */ fn is_solution(state: &mut Vec>>) -> bool { diff --git a/zh-hant/codes/rust/chapter_computational_complexity/space_complexity.rs b/zh-hant/codes/rust/chapter_computational_complexity/space_complexity.rs index d542f2be2..a21f08bdf 100644 --- a/zh-hant/codes/rust/chapter_computational_complexity/space_complexity.rs +++ b/zh-hant/codes/rust/chapter_computational_complexity/space_complexity.rs @@ -4,13 +4,10 @@ * Author: codingonion (coderonion@gmail.com) */ -include!("../include/include.rs"); - -use list_node::ListNode; +use hello_algo_rust::include::{print_util, ListNode, TreeNode}; use std::cell::RefCell; use std::collections::HashMap; use std::rc::Rc; -use tree_node::TreeNode; /* 函式 */ fn function() -> i32 { diff --git a/zh-hant/codes/rust/chapter_computational_complexity/worst_best_time_complexity.rs b/zh-hant/codes/rust/chapter_computational_complexity/worst_best_time_complexity.rs index f54e7c734..ab912d9ed 100644 --- a/zh-hant/codes/rust/chapter_computational_complexity/worst_best_time_complexity.rs +++ b/zh-hant/codes/rust/chapter_computational_complexity/worst_best_time_complexity.rs @@ -4,8 +4,7 @@ * Author: xBLACICEx (xBLACKICEx@outlook.com), codingonion (coderonion@gmail.com) */ -include!("../include/include.rs"); - +use hello_algo_rust::include::print_util; use rand::seq::SliceRandom; use rand::thread_rng; diff --git a/zh-hant/codes/rust/chapter_divide_and_conquer/build_tree.rs b/zh-hant/codes/rust/chapter_divide_and_conquer/build_tree.rs index 96aa1c61b..4bf9f73d8 100644 --- a/zh-hant/codes/rust/chapter_divide_and_conquer/build_tree.rs +++ b/zh-hant/codes/rust/chapter_divide_and_conquer/build_tree.rs @@ -4,10 +4,9 @@ * Author: codingonion (coderonion@gmail.com) */ +use hello_algo_rust::include::{print_util, TreeNode}; use std::collections::HashMap; use std::{cell::RefCell, rc::Rc}; -include!("../include/include.rs"); -use tree_node::TreeNode; /* 構建二元樹:分治 */ fn dfs( diff --git a/zh-hant/codes/rust/chapter_graph/graph_adjacency_list.rs b/zh-hant/codes/rust/chapter_graph/graph_adjacency_list.rs index 07b02caa6..c06e5fae9 100644 --- a/zh-hant/codes/rust/chapter_graph/graph_adjacency_list.rs +++ b/zh-hant/codes/rust/chapter_graph/graph_adjacency_list.rs @@ -4,7 +4,7 @@ * Author: night-cruise (2586447362@qq.com) */ -include!("../include/vertex.rs"); +pub use hello_algo_rust::include::{vals_to_vets, vets_to_vals, Vertex}; use std::collections::HashMap; diff --git a/zh-hant/codes/rust/chapter_hashing/build_in_hash.rs b/zh-hant/codes/rust/chapter_hashing/build_in_hash.rs index 47e327619..f4dee23bd 100644 --- a/zh-hant/codes/rust/chapter_hashing/build_in_hash.rs +++ b/zh-hant/codes/rust/chapter_hashing/build_in_hash.rs @@ -4,9 +4,8 @@ * Author: WSL0809 (wslzzy@outlook.com) */ -include!("../include/include.rs"); +use hello_algo_rust::include::ListNode; -use crate::list_node::ListNode; use std::collections::hash_map::DefaultHasher; use std::hash::{Hash, Hasher}; diff --git a/zh-hant/codes/rust/chapter_hashing/hash_map.rs b/zh-hant/codes/rust/chapter_hashing/hash_map.rs index ad32c45eb..330f66c8c 100644 --- a/zh-hant/codes/rust/chapter_hashing/hash_map.rs +++ b/zh-hant/codes/rust/chapter_hashing/hash_map.rs @@ -4,7 +4,7 @@ * Author: codingonion (coderonion@gmail.com) */ -include!("../include/include.rs"); +use hello_algo_rust::include::print_util; use std::collections::HashMap; diff --git a/zh-hant/codes/rust/chapter_heap/heap.rs b/zh-hant/codes/rust/chapter_heap/heap.rs index 979e83e14..1bf198cea 100644 --- a/zh-hant/codes/rust/chapter_heap/heap.rs +++ b/zh-hant/codes/rust/chapter_heap/heap.rs @@ -4,7 +4,7 @@ * Author: night-cruise (2586447362@qq.com) */ -include!("../include/include.rs"); +use hello_algo_rust::include::print_util; use std::collections::BinaryHeap; diff --git a/zh-hant/codes/rust/chapter_heap/my_heap.rs b/zh-hant/codes/rust/chapter_heap/my_heap.rs index 7a3652a8d..493986a7a 100644 --- a/zh-hant/codes/rust/chapter_heap/my_heap.rs +++ b/zh-hant/codes/rust/chapter_heap/my_heap.rs @@ -4,7 +4,7 @@ * Author: night-cruise (2586447362@qq.com) */ -include!("../include/include.rs"); +use hello_algo_rust::include::print_util; /* 大頂堆積 */ struct MaxHeap { diff --git a/zh-hant/codes/rust/chapter_heap/top_k.rs b/zh-hant/codes/rust/chapter_heap/top_k.rs index d978b7c8a..d16a3c28e 100644 --- a/zh-hant/codes/rust/chapter_heap/top_k.rs +++ b/zh-hant/codes/rust/chapter_heap/top_k.rs @@ -4,7 +4,7 @@ * Author: night-cruise (2586447362@qq.com) */ -include!("../include/include.rs"); +use hello_algo_rust::include::print_util; use std::cmp::Reverse; use std::collections::BinaryHeap; diff --git a/zh-hant/codes/rust/chapter_searching/hashing_search.rs b/zh-hant/codes/rust/chapter_searching/hashing_search.rs index 1f8e6ead4..d2b94c7a9 100644 --- a/zh-hant/codes/rust/chapter_searching/hashing_search.rs +++ b/zh-hant/codes/rust/chapter_searching/hashing_search.rs @@ -4,9 +4,7 @@ * Author: codingonion (coderonion@gmail.com) */ -include!("../include/include.rs"); - -use list_node::ListNode; +use hello_algo_rust::include::ListNode; use std::cell::RefCell; use std::collections::HashMap; use std::rc::Rc; diff --git a/zh-hant/codes/rust/chapter_searching/linear_search.rs b/zh-hant/codes/rust/chapter_searching/linear_search.rs index e731bf1d0..1ef589ccc 100644 --- a/zh-hant/codes/rust/chapter_searching/linear_search.rs +++ b/zh-hant/codes/rust/chapter_searching/linear_search.rs @@ -4,9 +4,7 @@ * Author: codingonion (coderonion@gmail.com) */ -include!("../include/include.rs"); - -use list_node::ListNode; +use hello_algo_rust::include::ListNode; use std::cell::RefCell; use std::rc::Rc; diff --git a/zh-hant/codes/rust/chapter_searching/two_sum.rs b/zh-hant/codes/rust/chapter_searching/two_sum.rs index 3cf044dbe..5192d3acc 100644 --- a/zh-hant/codes/rust/chapter_searching/two_sum.rs +++ b/zh-hant/codes/rust/chapter_searching/two_sum.rs @@ -4,8 +4,7 @@ * Author: xBLACICEx (xBLACKICEx@outlook.com), codingonion (coderonion@gmail.com) */ -include!("../include/include.rs"); - +use hello_algo_rust::include::print_util; use std::collections::HashMap; /* 方法一:暴力列舉 */ diff --git a/zh-hant/codes/rust/chapter_sorting/bubble_sort.rs b/zh-hant/codes/rust/chapter_sorting/bubble_sort.rs index f68602589..8a8902317 100644 --- a/zh-hant/codes/rust/chapter_sorting/bubble_sort.rs +++ b/zh-hant/codes/rust/chapter_sorting/bubble_sort.rs @@ -4,7 +4,7 @@ * Author: codingonion (coderonion@gmail.com) */ -include!("../include/include.rs"); +use hello_algo_rust::include::print_util; /* 泡沫排序 */ fn bubble_sort(nums: &mut [i32]) { diff --git a/zh-hant/codes/rust/chapter_sorting/bucket_sort.rs b/zh-hant/codes/rust/chapter_sorting/bucket_sort.rs index c85d64f87..5efbebe1d 100644 --- a/zh-hant/codes/rust/chapter_sorting/bucket_sort.rs +++ b/zh-hant/codes/rust/chapter_sorting/bucket_sort.rs @@ -4,7 +4,7 @@ * Author: night-cruise (2586447362@qq.com) */ -include!("../include/include.rs"); +use hello_algo_rust::include::print_util; /* 桶排序 */ fn bucket_sort(nums: &mut [f64]) { diff --git a/zh-hant/codes/rust/chapter_sorting/counting_sort.rs b/zh-hant/codes/rust/chapter_sorting/counting_sort.rs index e854b88bb..eac4c3f6d 100644 --- a/zh-hant/codes/rust/chapter_sorting/counting_sort.rs +++ b/zh-hant/codes/rust/chapter_sorting/counting_sort.rs @@ -4,7 +4,7 @@ * Author: night-cruise (2586447362@qq.com) */ -include!("../include/include.rs"); +use hello_algo_rust::include::print_util; /* 計數排序 */ // 簡單實現,無法用於排序物件 diff --git a/zh-hant/codes/rust/chapter_sorting/heap_sort.rs b/zh-hant/codes/rust/chapter_sorting/heap_sort.rs index 2d71c348c..db1cbc95c 100644 --- a/zh-hant/codes/rust/chapter_sorting/heap_sort.rs +++ b/zh-hant/codes/rust/chapter_sorting/heap_sort.rs @@ -4,7 +4,7 @@ * Author: night-cruise (2586447362@qq.com) */ -include!("../include/include.rs"); +use hello_algo_rust::include::print_util; /* 堆積的長度為 n ,從節點 i 開始,從頂至底堆積化 */ fn sift_down(nums: &mut [i32], n: usize, mut i: usize) { diff --git a/zh-hant/codes/rust/chapter_sorting/insertion_sort.rs b/zh-hant/codes/rust/chapter_sorting/insertion_sort.rs index a804c668a..f99716f87 100644 --- a/zh-hant/codes/rust/chapter_sorting/insertion_sort.rs +++ b/zh-hant/codes/rust/chapter_sorting/insertion_sort.rs @@ -4,7 +4,7 @@ * Author: xBLACKICEx (xBLACKICEx@outlook.com) */ -include!("../include/include.rs"); +use hello_algo_rust::include::print_util; /* 插入排序 */ fn insertion_sort(nums: &mut [i32]) { diff --git a/zh-hant/codes/rust/chapter_sorting/radix_sort.rs b/zh-hant/codes/rust/chapter_sorting/radix_sort.rs index d818a58f1..b991e915f 100644 --- a/zh-hant/codes/rust/chapter_sorting/radix_sort.rs +++ b/zh-hant/codes/rust/chapter_sorting/radix_sort.rs @@ -4,7 +4,7 @@ * Author: night-cruise (2586447362@qq.com) */ -include!("../include/include.rs"); +use hello_algo_rust::include::print_util; /* 獲取元素 num 的第 k 位,其中 exp = 10^(k-1) */ fn digit(num: i32, exp: i32) -> usize { diff --git a/zh-hant/codes/rust/chapter_sorting/selection_sort.rs b/zh-hant/codes/rust/chapter_sorting/selection_sort.rs index 70f51dce0..9c47336a7 100644 --- a/zh-hant/codes/rust/chapter_sorting/selection_sort.rs +++ b/zh-hant/codes/rust/chapter_sorting/selection_sort.rs @@ -4,7 +4,7 @@ * Author: WSL0809 (wslzzy@outlook.com) */ -include!("../include/include.rs"); +use hello_algo_rust::include::print_util; /* 選擇排序 */ fn selection_sort(nums: &mut [i32]) { diff --git a/zh-hant/codes/rust/chapter_stack_and_queue/array_deque.rs b/zh-hant/codes/rust/chapter_stack_and_queue/array_deque.rs index f9308ea7d..5dbe16ef1 100644 --- a/zh-hant/codes/rust/chapter_stack_and_queue/array_deque.rs +++ b/zh-hant/codes/rust/chapter_stack_and_queue/array_deque.rs @@ -3,9 +3,7 @@ * Created Time: 2023-03-11 * Author: codingonion (coderonion@gmail.com) */ - -include!("../include/include.rs"); - +use hello_algo_rust::include::print_util; /* 基於環形陣列實現的雙向佇列 */ struct ArrayDeque { nums: Vec, // 用於儲存雙向佇列元素的陣列 diff --git a/zh-hant/codes/rust/chapter_stack_and_queue/array_stack.rs b/zh-hant/codes/rust/chapter_stack_and_queue/array_stack.rs index 8eaa0117e..6a850f8c7 100644 --- a/zh-hant/codes/rust/chapter_stack_and_queue/array_stack.rs +++ b/zh-hant/codes/rust/chapter_stack_and_queue/array_stack.rs @@ -4,7 +4,7 @@ * Author: WSL0809 (wslzzy@outlook.com), codingonion (coderonion@gmail.com) */ -include!("../include/include.rs"); +use hello_algo_rust::include::print_util; /* 基於陣列實現的堆疊 */ struct ArrayStack { diff --git a/zh-hant/codes/rust/chapter_stack_and_queue/deque.rs b/zh-hant/codes/rust/chapter_stack_and_queue/deque.rs index 287808cc1..e5ad4abe1 100644 --- a/zh-hant/codes/rust/chapter_stack_and_queue/deque.rs +++ b/zh-hant/codes/rust/chapter_stack_and_queue/deque.rs @@ -4,8 +4,7 @@ * Author: codingonion (coderonion@gmail.com), xBLACKICEx (xBLACKICEx@outlook.com) */ -include!("../include/include.rs"); - +use hello_algo_rust::include::print_util; use std::collections::VecDeque; /* Driver Code */ diff --git a/zh-hant/codes/rust/chapter_stack_and_queue/linkedlist_deque.rs b/zh-hant/codes/rust/chapter_stack_and_queue/linkedlist_deque.rs index 93e288531..bbc7a5588 100644 --- a/zh-hant/codes/rust/chapter_stack_and_queue/linkedlist_deque.rs +++ b/zh-hant/codes/rust/chapter_stack_and_queue/linkedlist_deque.rs @@ -4,7 +4,7 @@ * Author: codingonion (coderonion@gmail.com) */ -include!("../include/include.rs"); +use hello_algo_rust::include::print_util; use std::cell::RefCell; use std::rc::Rc; diff --git a/zh-hant/codes/rust/chapter_stack_and_queue/linkedlist_queue.rs b/zh-hant/codes/rust/chapter_stack_and_queue/linkedlist_queue.rs index d12329002..4e114e70d 100644 --- a/zh-hant/codes/rust/chapter_stack_and_queue/linkedlist_queue.rs +++ b/zh-hant/codes/rust/chapter_stack_and_queue/linkedlist_queue.rs @@ -4,9 +4,8 @@ * Author: codingonion (coderonion@gmail.com) */ -include!("../include/include.rs"); +use hello_algo_rust::include::{print_util, ListNode}; -use list_node::ListNode; use std::cell::RefCell; use std::rc::Rc; diff --git a/zh-hant/codes/rust/chapter_stack_and_queue/linkedlist_stack.rs b/zh-hant/codes/rust/chapter_stack_and_queue/linkedlist_stack.rs index d5ca545ba..ac07580fb 100644 --- a/zh-hant/codes/rust/chapter_stack_and_queue/linkedlist_stack.rs +++ b/zh-hant/codes/rust/chapter_stack_and_queue/linkedlist_stack.rs @@ -4,9 +4,8 @@ * Author: codingonion (coderonion@gmail.com) */ -include!("../include/include.rs"); +use hello_algo_rust::include::{print_util, ListNode}; -use list_node::ListNode; use std::cell::RefCell; use std::rc::Rc; diff --git a/zh-hant/codes/rust/chapter_stack_and_queue/queue.rs b/zh-hant/codes/rust/chapter_stack_and_queue/queue.rs index 10e0d1b59..2605a4111 100644 --- a/zh-hant/codes/rust/chapter_stack_and_queue/queue.rs +++ b/zh-hant/codes/rust/chapter_stack_and_queue/queue.rs @@ -4,7 +4,7 @@ * Author: codingonion (coderonion@gmail.com), xBLACKICEx (xBLACKICEx@outlook.com) */ -include!("../include/include.rs"); +use hello_algo_rust::include::print_util; use std::collections::VecDeque; diff --git a/zh-hant/codes/rust/chapter_stack_and_queue/stack.rs b/zh-hant/codes/rust/chapter_stack_and_queue/stack.rs index cc871dbdd..83f895c71 100644 --- a/zh-hant/codes/rust/chapter_stack_and_queue/stack.rs +++ b/zh-hant/codes/rust/chapter_stack_and_queue/stack.rs @@ -4,7 +4,7 @@ * Author: codingonion (coderonion@gmail.com) */ -include!("../include/include.rs"); +use hello_algo_rust::include::print_util; /* Driver Code */ pub fn main() { diff --git a/zh-hant/codes/rust/chapter_tree/array_binary_tree.rs b/zh-hant/codes/rust/chapter_tree/array_binary_tree.rs index 2a60242a3..a7f32721d 100644 --- a/zh-hant/codes/rust/chapter_tree/array_binary_tree.rs +++ b/zh-hant/codes/rust/chapter_tree/array_binary_tree.rs @@ -4,7 +4,7 @@ * Author: night-cruise (2586447362@qq.com) */ -include!("../include/include.rs"); +use hello_algo_rust::include::{print_util, tree_node}; /* 陣列表示下的二元樹類別 */ struct ArrayBinaryTree { @@ -49,18 +49,11 @@ impl ArrayBinaryTree { /* 層序走訪 */ fn level_order(&self) -> Vec { - let mut res = vec![]; - // 直接走訪陣列 - for i in 0..self.size() { - if let Some(val) = self.val(i) { - res.push(val) - } - } - res + self.tree.iter().filter_map(|&x| x).collect() } /* 深度優先走訪 */ - fn dfs(&self, i: i32, order: &str, res: &mut Vec) { + fn dfs(&self, i: i32, order: &'static str, res: &mut Vec) { if self.val(i).is_none() { return; } diff --git a/zh-hant/codes/rust/chapter_tree/avl_tree.rs b/zh-hant/codes/rust/chapter_tree/avl_tree.rs index c414a06e4..eb48530cf 100644 --- a/zh-hant/codes/rust/chapter_tree/avl_tree.rs +++ b/zh-hant/codes/rust/chapter_tree/avl_tree.rs @@ -4,12 +4,11 @@ * Author: night-cruise (2586447362@qq.com) */ -include!("../include/include.rs"); +use hello_algo_rust::include::{print_util, TreeNode}; use std::cell::RefCell; use std::cmp::Ordering; use std::rc::Rc; -use tree_node::TreeNode; type OptionTreeNodeRc = Option>>; diff --git a/zh-hant/codes/rust/chapter_tree/binary_search_tree.rs b/zh-hant/codes/rust/chapter_tree/binary_search_tree.rs index 99bf6ae61..1186f6bca 100644 --- a/zh-hant/codes/rust/chapter_tree/binary_search_tree.rs +++ b/zh-hant/codes/rust/chapter_tree/binary_search_tree.rs @@ -4,13 +4,13 @@ * Author: xBLACKICEx (xBLACKICE@outlook.com)、night-cruise (2586447362@qq.com) */ -include!("../include/include.rs"); +use hello_algo_rust::include::print_util; use std::cell::RefCell; use std::cmp::Ordering; use std::rc::Rc; -use tree_node::TreeNode; +use hello_algo_rust::include::TreeNode; type OptionTreeNodeRc = Option>>; @@ -126,7 +126,7 @@ impl BinarySearchTree { // 刪除節點 cur if !Rc::ptr_eq(&cur, self.root.as_ref().unwrap()) { let left = pre.borrow().left.clone(); - if left.is_some() && Rc::ptr_eq(&left.as_ref().unwrap(), &cur) { + if left.is_some() && Rc::ptr_eq(left.as_ref().unwrap(), &cur) { pre.borrow_mut().left = child; } else { pre.borrow_mut().right = child; @@ -147,11 +147,11 @@ impl BinarySearchTree { break; } } - let tmpval = tmp.unwrap().borrow().val; + let tmp_val = tmp.unwrap().borrow().val; // 遞迴刪除節點 tmp - self.remove(tmpval); + self.remove(tmp_val); // 用 tmp 覆蓋 cur - cur.borrow_mut().val = tmpval; + cur.borrow_mut().val = tmp_val; } } } diff --git a/zh-hant/codes/rust/chapter_tree/binary_tree.rs b/zh-hant/codes/rust/chapter_tree/binary_tree.rs index f87b7f891..22da1089a 100644 --- a/zh-hant/codes/rust/chapter_tree/binary_tree.rs +++ b/zh-hant/codes/rust/chapter_tree/binary_tree.rs @@ -4,8 +4,7 @@ * Author: xBLACKICEx (xBLACKICE@outlook.com) */ use std::rc::Rc; -include!("../include/include.rs"); -use tree_node::TreeNode; +use hello_algo_rust::include::{print_util, TreeNode}; /* Driver Code */ fn main() { diff --git a/zh-hant/codes/rust/chapter_tree/binary_tree_bfs.rs b/zh-hant/codes/rust/chapter_tree/binary_tree_bfs.rs index 01a19adfd..20ef8b005 100644 --- a/zh-hant/codes/rust/chapter_tree/binary_tree_bfs.rs +++ b/zh-hant/codes/rust/chapter_tree/binary_tree_bfs.rs @@ -4,11 +4,11 @@ * Author: xBLACKICEx (xBLACKICE@outlook.com) */ -include!("../include/include.rs"); +use hello_algo_rust::include::{print_util, vec_to_tree, TreeNode}; +use hello_algo_rust::op_vec; use std::collections::VecDeque; use std::{cell::RefCell, rc::Rc}; -use tree_node::{vec_to_tree, TreeNode}; /* 層序走訪 */ fn level_order(root: &Rc>) -> Vec { diff --git a/zh-hant/codes/rust/chapter_tree/binary_tree_dfs.rs b/zh-hant/codes/rust/chapter_tree/binary_tree_dfs.rs index dc0c00564..35f236d0a 100644 --- a/zh-hant/codes/rust/chapter_tree/binary_tree_dfs.rs +++ b/zh-hant/codes/rust/chapter_tree/binary_tree_dfs.rs @@ -4,22 +4,27 @@ * Author: xBLACKICEx (xBLACKICE@outlook.com) */ -include!("../include/include.rs"); +use hello_algo_rust::include::{print_util, vec_to_tree, TreeNode}; +use hello_algo_rust::op_vec; use std::cell::RefCell; use std::rc::Rc; -use tree_node::{vec_to_tree, TreeNode}; /* 前序走訪 */ fn pre_order(root: Option<&Rc>>) -> Vec { let mut result = vec![]; - if let Some(node) = root { - // 訪問優先順序:根節點 -> 左子樹 -> 右子樹 - result.push(node.borrow().val); - result.extend(pre_order(node.borrow().left.as_ref())); - result.extend(pre_order(node.borrow().right.as_ref())); + fn dfs(root: Option<&Rc>>, res: &mut Vec) { + if let Some(node) = root { + // 訪問優先順序:根節點 -> 左子樹 -> 右子樹 + let node = node.borrow(); + res.push(node.val); + dfs(node.left.as_ref(), res); + dfs(node.right.as_ref(), res); + } } + dfs(root, &mut result); + result } @@ -27,12 +32,17 @@ fn pre_order(root: Option<&Rc>>) -> Vec { fn in_order(root: Option<&Rc>>) -> Vec { let mut result = vec![]; - if let Some(node) = root { - // 訪問優先順序:左子樹 -> 根節點 -> 右子樹 - result.extend(in_order(node.borrow().left.as_ref())); - result.push(node.borrow().val); - result.extend(in_order(node.borrow().right.as_ref())); + fn dfs(root: Option<&Rc>>, res: &mut Vec) { + if let Some(node) = root { + // 訪問優先順序:左子樹 -> 根節點 -> 右子樹 + let node = node.borrow(); + dfs(node.left.as_ref(), res); + res.push(node.val); + dfs(node.right.as_ref(), res); + } } + dfs(root, &mut result); + result } @@ -40,12 +50,18 @@ fn in_order(root: Option<&Rc>>) -> Vec { fn post_order(root: Option<&Rc>>) -> Vec { let mut result = vec![]; - if let Some(node) = root { - // 訪問優先順序:左子樹 -> 右子樹 -> 根節點 - result.extend(post_order(node.borrow().left.as_ref())); - result.extend(post_order(node.borrow().right.as_ref())); - result.push(node.borrow().val); + fn dfs(root: Option<&Rc>>, res: &mut Vec) { + if let Some(node) = root { + // 訪問優先順序:左子樹 -> 右子樹 -> 根節點 + let node = node.borrow(); + dfs(node.left.as_ref(), res); + dfs(node.right.as_ref(), res); + res.push(node.val); + } } + + dfs(root, &mut result); + result } diff --git a/zh-hant/codes/rust/src/include/list_node.rs b/zh-hant/codes/rust/src/include/list_node.rs new file mode 100644 index 000000000..941e334c3 --- /dev/null +++ b/zh-hant/codes/rust/src/include/list_node.rs @@ -0,0 +1,57 @@ +/* + * File: list_node.rs + * Created Time: 2023-03-05 + * Author: codingonion (coderonion@gmail.com), rongyi (hiarongyi@gmail.com) + */ + +use std::cell::RefCell; +use std::collections::HashMap; +use std::rc::Rc; + +#[derive(Debug)] +pub struct ListNode { + pub val: T, + pub next: Option>>>, +} + +impl ListNode { + pub fn new(val: T) -> Rc>> { + Rc::new(RefCell::new(ListNode { val, next: None })) + } + + /* 將陣列反序列化為鏈結串列 */ + pub fn arr_to_linked_list(array: &[T]) -> Option>>> + where + T: Copy + Clone, + { + let mut head = None; + // insert in reverse order + for item in array.iter().rev() { + let node = Rc::new(RefCell::new(ListNode { + val: *item, + next: head.take(), + })); + head = Some(node); + } + head + } + + /* 將鏈結串列轉化為雜湊表 */ + pub fn linked_list_to_hashmap( + linked_list: Option>>>, + ) -> HashMap>>> + where + T: std::hash::Hash + Eq + Copy + Clone, + { + let mut hashmap = HashMap::new(); + let mut node = linked_list; + + while let Some(cur) = node { + let borrow = cur.borrow(); + hashmap.insert(borrow.val.clone(), cur.clone()); + node = borrow.next.clone(); + } + + hashmap + } +} diff --git a/zh-hant/codes/rust/src/include/mod.rs b/zh-hant/codes/rust/src/include/mod.rs new file mode 100644 index 000000000..6cba6f9a5 --- /dev/null +++ b/zh-hant/codes/rust/src/include/mod.rs @@ -0,0 +1,16 @@ +/* + * File: include.rs + * Created Time: 2023-02-05 + * Author: codingonion (coderonion@gmail.com), xBLACKICEx (xBLACKICE@outlook.com) + */ + +pub mod list_node; +pub mod print_util; +pub mod tree_node; +pub mod vertex; + +// rexport to include +pub use list_node::*; +pub use print_util::*; +pub use tree_node::*; +pub use vertex::*; diff --git a/zh-hant/codes/rust/src/include/print_util.rs b/zh-hant/codes/rust/src/include/print_util.rs new file mode 100644 index 000000000..fd93f3626 --- /dev/null +++ b/zh-hant/codes/rust/src/include/print_util.rs @@ -0,0 +1,103 @@ +/* + * File: print_util.rs + * Created Time: 2023-02-05 + * Author: codingonion (coderonion@gmail.com), xBLACKICEx (xBLACKICEx@outlook.com) + */ + +use std::cell::{Cell, RefCell}; +use std::fmt::Display; +use std::collections::{HashMap, VecDeque}; +use std::rc::Rc; + +use super::list_node::ListNode; +use super::tree_node::{TreeNode, vec_to_tree}; + +struct Trunk<'a, 'b> { + prev: Option<&'a Trunk<'a, 'b>>, + str: Cell<&'b str>, +} + +/* 列印陣列 */ +pub fn print_array(nums: &[T]) { + print!("["); + if nums.len() > 0 { + for (i, num) in nums.iter().enumerate() { + print!("{}{}", num, if i == nums.len() - 1 {"]"} else {", "} ); + } + } else { + print!("]"); + } +} + +/* 列印雜湊表 */ +pub fn print_hash_map(map: &HashMap) { + for (key, value) in map { + println!("{key} -> {value}"); + } +} + +/* 列印佇列(雙向佇列) */ +pub fn print_queue(queue: &VecDeque) { + print!("["); + let iter = queue.iter(); + for (i, data) in iter.enumerate() { + print!("{}{}", data, if i == queue.len() - 1 {"]"} else {", "} ); + } +} + +/* 列印鏈結串列 */ +pub fn print_linked_list(head: &Rc>>) { + print!("{}{}", head.borrow().val, if head.borrow().next.is_none() {"\n"} else {" -> "}); + if let Some(node) = &head.borrow().next { + return print_linked_list(node); + } +} + +/* 列印二元樹 */ +pub fn print_tree(root: &Rc>) { + _print_tree(Some(root), None, false); +} + +/* 列印二元樹 */ +fn _print_tree(root: Option<&Rc>>, prev: Option<&Trunk>, is_right: bool) { + if let Some(node) = root { + let mut prev_str = " "; + let trunk = Trunk { prev, str: Cell::new(prev_str) }; + _print_tree(node.borrow().right.as_ref(), Some(&trunk), true); + + if prev.is_none() { + trunk.str.set("———"); + } else if is_right { + trunk.str.set("/———"); + prev_str = " |"; + } else { + trunk.str.set("\\———"); + prev.as_ref().unwrap().str.set(prev_str); + } + + show_trunks(Some(&trunk)); + println!(" {}", node.borrow().val); + if let Some(prev) = prev { + prev.str.set(prev_str); + } + trunk.str.set(" |"); + + _print_tree(node.borrow().left.as_ref(), Some(&trunk), false); + } +} + +fn show_trunks(trunk: Option<&Trunk>) { + if let Some(trunk) = trunk { + show_trunks(trunk.prev); + print!("{}", trunk.str.get()); + } +} + +/* 列印堆積 */ +pub fn print_heap(heap: Vec) { + println!("堆積的陣列表示:{:?}", heap); + println!("堆積的樹狀表示:"); + if let Some(root) = vec_to_tree(heap.into_iter().map(|val| Some(val)).collect()) { + print_tree(&root); + } +} diff --git a/zh-hant/codes/rust/src/include/tree_node.rs b/zh-hant/codes/rust/src/include/tree_node.rs new file mode 100644 index 000000000..514491862 --- /dev/null +++ b/zh-hant/codes/rust/src/include/tree_node.rs @@ -0,0 +1,92 @@ +/* + * File: tree_node.rs + * Created Time: 2023-02-27 + * Author: xBLACKICEx (xBLACKICE@outlook.com), night-cruise (2586447362@qq.com) + */ + +use std::cell::RefCell; +use std::rc::Rc; + +/* 二元樹節點型別 */ +#[derive(Debug)] +pub struct TreeNode { + pub val: i32, + pub height: i32, + pub parent: Option>>, + pub left: Option>>, + pub right: Option>>, +} + +impl TreeNode { + /* 建構子 */ + pub fn new(val: i32) -> Rc> { + Rc::new(RefCell::new(Self { + val, + height: 0, + parent: None, + left: None, + right: None, + })) + } +} + +#[macro_export] +macro_rules! op_vec { + ( $( $x:expr ),* ) => { + vec![ + $( Option::from($x).map(|x| x) ),* + ] + }; +} + +// 序列化編碼規則請參考: +// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/ +// 二元樹的陣列表示: +// [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15] +// 二元樹的鏈結串列表示: +// /——— 15 +// /——— 7 +// /——— 3 +// | \——— 6 +// | \——— 12 +// ——— 1 +// \——— 2 +// | /——— 9 +// \——— 4 +// \——— 8 + +/* 將串列反序列化為二元樹:遞迴 */ +fn vec_to_tree_dfs(arr: &[Option], i: usize) -> Option>> { + if i >= arr.len() || arr[i].is_none() { + return None; + } + let root = TreeNode::new(arr[i].unwrap()); + root.borrow_mut().left = vec_to_tree_dfs(arr, 2 * i + 1); + root.borrow_mut().right = vec_to_tree_dfs(arr, 2 * i + 2); + Some(root) +} + +/* 將串列反序列化為二元樹 */ +pub fn vec_to_tree(arr: Vec>) -> Option>> { + vec_to_tree_dfs(&arr, 0) +} + +/* 將二元樹序列化為串列:遞迴 */ +fn tree_to_vec_dfs(root: Option<&Rc>>, i: usize, res: &mut Vec>) { + if let Some(root) = root { + // i + 1 is the minimum valid size to access index i + while res.len() < i + 1 { + res.push(None); + } + res[i] = Some(root.borrow().val); + tree_to_vec_dfs(root.borrow().left.as_ref(), 2 * i + 1, res); + tree_to_vec_dfs(root.borrow().right.as_ref(), 2 * i + 2, res); + } +} + +/* 將二元樹序列化為串列 */ +pub fn tree_to_vec(root: Option>>) -> Vec> { + let mut res = vec![]; + tree_to_vec_dfs(root.as_ref(), 0, &mut res); + res +} diff --git a/zh-hant/codes/rust/src/include/vertex.rs b/zh-hant/codes/rust/src/include/vertex.rs new file mode 100644 index 000000000..6d9b5e550 --- /dev/null +++ b/zh-hant/codes/rust/src/include/vertex.rs @@ -0,0 +1,21 @@ +/* + * File: vertex.rs + * Created Time: 2023-07-13 + * Author: night-cruise (2586447362@qq.com) + */ + +/* 頂點型別 */ +#[derive(Copy, Clone, Hash, PartialEq, Eq)] +pub struct Vertex { + pub val: i32, +} + +/* 輸入值串列 vals ,返回頂點串列 vets */ +pub fn vals_to_vets(vals: Vec) -> Vec { + vals.into_iter().map(|val| Vertex { val }).collect() +} + +/* 輸入頂點串列 vets ,返回值串列 vals */ +pub fn vets_to_vals(vets: Vec) -> Vec { + vets.into_iter().map(|vet| vet.val).collect() +} diff --git a/zh-hant/codes/rust/src/lib.rs b/zh-hant/codes/rust/src/lib.rs new file mode 100644 index 000000000..2883b9104 --- /dev/null +++ b/zh-hant/codes/rust/src/lib.rs @@ -0,0 +1 @@ +pub mod include; diff --git a/zh-hant/codes/swift/chapter_dynamic_programming/min_path_sum.swift b/zh-hant/codes/swift/chapter_dynamic_programming/min_path_sum.swift index f2621f089..ca3048e04 100644 --- a/zh-hant/codes/swift/chapter_dynamic_programming/min_path_sum.swift +++ b/zh-hant/codes/swift/chapter_dynamic_programming/min_path_sum.swift @@ -105,19 +105,19 @@ enum MinPathSum { // 暴力搜尋 var res = minPathSumDFS(grid: grid, i: n - 1, j: m - 1) - print("從左上角到右下角的做小路徑和為 \(res)") + print("從左上角到右下角的最小路徑和為 \(res)") // 記憶化搜尋 var mem = Array(repeating: Array(repeating: -1, count: m), count: n) res = minPathSumDFSMem(grid: grid, mem: &mem, i: n - 1, j: m - 1) - print("從左上角到右下角的做小路徑和為 \(res)") + print("從左上角到右下角的最小路徑和為 \(res)") // 動態規劃 res = minPathSumDP(grid: grid) - print("從左上角到右下角的做小路徑和為 \(res)") + print("從左上角到右下角的最小路徑和為 \(res)") // 空間最佳化後的動態規劃 res = minPathSumDPComp(grid: grid) - print("從左上角到右下角的做小路徑和為 \(res)") + print("從左上角到右下角的最小路徑和為 \(res)") } } diff --git a/zh-hant/docs/chapter_heap/heap.md b/zh-hant/docs/chapter_heap/heap.md index 7d04a45d3..00a321b4f 100644 --- a/zh-hant/docs/chapter_heap/heap.md +++ b/zh-hant/docs/chapter_heap/heap.md @@ -157,8 +157,8 @@ /* 初始化堆積 */ // 初始化小頂堆積 PriorityQueue minHeap = new(); - // 初始化大頂堆積(使用 lambda 表示式修改 Comparator 即可) - PriorityQueue maxHeap = new(Comparer.Create((x, y) => y - x)); + // 初始化大頂堆積(使用 lambda 表示式修改 Comparer 即可) + PriorityQueue maxHeap = new(Comparer.Create((x, y) => y.CompareTo(x))); /* 元素入堆積 */ maxHeap.Enqueue(1, 1); diff --git a/zh-hant/docs/chapter_searching/replace_linear_by_hashing.assets/two_sum_brute_force.png b/zh-hant/docs/chapter_searching/replace_linear_by_hashing.assets/two_sum_brute_force.png index 74017260e..985012db7 100644 Binary files a/zh-hant/docs/chapter_searching/replace_linear_by_hashing.assets/two_sum_brute_force.png and b/zh-hant/docs/chapter_searching/replace_linear_by_hashing.assets/two_sum_brute_force.png differ diff --git a/zh-hant/docs/chapter_tree/avl_tree.md b/zh-hant/docs/chapter_tree/avl_tree.md index 38da5155b..1a2c0a93d 100644 --- a/zh-hant/docs/chapter_tree/avl_tree.md +++ b/zh-hant/docs/chapter_tree/avl_tree.md @@ -180,7 +180,7 @@ AVL 樹既是二元搜尋樹,也是平衡二元樹,同時滿足這兩類二 ```c title="" /* AVL 樹節點結構體 */ - TreeNode struct TreeNode { + typedef struct TreeNode { int val; int height; struct TreeNode *left; diff --git a/zh-hant/docs/index.html b/zh-hant/docs/index.html index 681f66b53..10919b59f 100644 --- a/zh-hant/docs/index.html +++ b/zh-hant/docs/index.html @@ -372,4 +372,4 @@ - + \ No newline at end of file