From 60162f6fa812e2d0f1d6334b0895d86c40971c47 Mon Sep 17 00:00:00 2001 From: krahets Date: Wed, 26 Jul 2023 10:01:49 +0800 Subject: [PATCH] Finetune Rust code. --- .../chapter_array_and_linkedlist/my_list.rs | 1 + .../chapter_divide_and_conquer/build_tree.rs | 19 +++++++++---------- .../climbing_stairs_backtrack.rs | 2 +- .../climbing_stairs_constraint_dp.rs | 2 +- .../climbing_stairs_dfs.rs | 2 +- .../climbing_stairs_dp.rs | 2 +- .../min_cost_climbing_stairs_dp.rs | 2 +- .../chapter_graph/graph_adjacency_list.rs | 2 +- .../chapter_graph/graph_adjacency_matrix.rs | 2 +- codes/rust/chapter_graph/graph_bfs.rs | 2 +- codes/rust/chapter_graph/graph_dfs.rs | 2 +- .../rust/chapter_greedy/coin_change_greedy.rs | 2 +- .../chapter_greedy/fractional_knapsack.rs | 2 +- codes/rust/chapter_greedy/max_capacity.rs | 2 +- codes/rust/chapter_hashing/array_hash_map.rs | 6 ++++-- codes/rust/chapter_heap/heap.rs | 2 +- .../chapter_stack_and_queue/array_deque.rs | 1 + .../chapter_stack_and_queue/array_queue.rs | 1 + 18 files changed, 29 insertions(+), 25 deletions(-) diff --git a/codes/rust/chapter_array_and_linkedlist/my_list.rs b/codes/rust/chapter_array_and_linkedlist/my_list.rs index 443ded340..c2f63dd70 100644 --- a/codes/rust/chapter_array_and_linkedlist/my_list.rs +++ b/codes/rust/chapter_array_and_linkedlist/my_list.rs @@ -7,6 +7,7 @@ include!("../include/include.rs"); #[allow(dead_code)] +/* 列表类简易实现 */ struct MyList { nums: Vec, // 数组(存储列表元素) capacity: usize, // 列表容量 diff --git a/codes/rust/chapter_divide_and_conquer/build_tree.rs b/codes/rust/chapter_divide_and_conquer/build_tree.rs index b31a91381..3478e3ab6 100644 --- a/codes/rust/chapter_divide_and_conquer/build_tree.rs +++ b/codes/rust/chapter_divide_and_conquer/build_tree.rs @@ -4,11 +4,11 @@ * Author: sjinzh (sjinzh@gmail.com) */ - use std::{cell::RefCell, rc::Rc}; - use std::collections::HashMap; - include!("../include/include.rs"); - use tree_node::TreeNode; - +use std::{cell::RefCell, rc::Rc}; +use std::collections::HashMap; +include!("../include/include.rs"); +use tree_node::TreeNode; + /* 构建二叉树:分治 */ fn dfs(preorder: &[i32], inorder: &[i32], hmap: &HashMap, i: i32, l: i32, r: i32) -> Option>> { // 子树区间为空时终止 @@ -26,7 +26,7 @@ fn dfs(preorder: &[i32], inorder: &[i32], hmap: &HashMap, i: i32, l: i } /* 构建二叉树 */ - fn build_tree(preorder: &[i32], inorder: &[i32]) -> Option>> { +fn build_tree(preorder: &[i32], inorder: &[i32]) -> Option>> { // 初始化哈希表,存储 inorder 元素到索引的映射 let mut hmap: HashMap = HashMap::new(); for i in 0..inorder.len() { @@ -36,8 +36,8 @@ fn dfs(preorder: &[i32], inorder: &[i32], hmap: &HashMap, i: i32, l: i root } - /* Driver Code */ - fn main() { +/* Driver Code */ +fn main() { let preorder = [ 3, 9, 2, 1, 7 ]; let inorder = [ 9, 3, 1, 2, 7 ]; println!("中序遍历 = {:?}", preorder); @@ -46,5 +46,4 @@ fn dfs(preorder: &[i32], inorder: &[i32], hmap: &HashMap, i: i32, l: i let root = build_tree(&preorder, &inorder); println!("构建的二叉树为:"); print_util::print_tree(root.as_ref().unwrap()); - } - \ No newline at end of file +} diff --git a/codes/rust/chapter_dynamic_programming/climbing_stairs_backtrack.rs b/codes/rust/chapter_dynamic_programming/climbing_stairs_backtrack.rs index ba0270717..1e9443491 100644 --- a/codes/rust/chapter_dynamic_programming/climbing_stairs_backtrack.rs +++ b/codes/rust/chapter_dynamic_programming/climbing_stairs_backtrack.rs @@ -34,4 +34,4 @@ pub fn main() { let res = climbing_stairs_backtrack(n); println!("爬 {n} 阶楼梯共有 {res} 种方案"); -} \ No newline at end of file +} diff --git a/codes/rust/chapter_dynamic_programming/climbing_stairs_constraint_dp.rs b/codes/rust/chapter_dynamic_programming/climbing_stairs_constraint_dp.rs index 793e15e29..675e050c8 100644 --- a/codes/rust/chapter_dynamic_programming/climbing_stairs_constraint_dp.rs +++ b/codes/rust/chapter_dynamic_programming/climbing_stairs_constraint_dp.rs @@ -28,4 +28,4 @@ pub fn main() { let res = climbing_stairs_constraint_dp(n); println!("爬 {n} 阶楼梯共有 {res} 种方案"); -} \ No newline at end of file +} diff --git a/codes/rust/chapter_dynamic_programming/climbing_stairs_dfs.rs b/codes/rust/chapter_dynamic_programming/climbing_stairs_dfs.rs index c5e9b1ac1..61ad3160e 100644 --- a/codes/rust/chapter_dynamic_programming/climbing_stairs_dfs.rs +++ b/codes/rust/chapter_dynamic_programming/climbing_stairs_dfs.rs @@ -24,4 +24,4 @@ pub fn main() { let res = climbing_stairs_dfs(n); println!("爬 {n} 阶楼梯共有 {res} 种方案"); -} \ No newline at end of file +} diff --git a/codes/rust/chapter_dynamic_programming/climbing_stairs_dp.rs b/codes/rust/chapter_dynamic_programming/climbing_stairs_dp.rs index 75bbe79df..9e85e809b 100644 --- a/codes/rust/chapter_dynamic_programming/climbing_stairs_dp.rs +++ b/codes/rust/chapter_dynamic_programming/climbing_stairs_dp.rs @@ -41,4 +41,4 @@ pub fn main() { let res = climbing_stairs_dp_comp(n); println!("爬 {n} 阶楼梯共有 {res} 种方案"); -} \ No newline at end of file +} diff --git a/codes/rust/chapter_dynamic_programming/min_cost_climbing_stairs_dp.rs b/codes/rust/chapter_dynamic_programming/min_cost_climbing_stairs_dp.rs index c6b9c029c..74d527e54 100644 --- a/codes/rust/chapter_dynamic_programming/min_cost_climbing_stairs_dp.rs +++ b/codes/rust/chapter_dynamic_programming/min_cost_climbing_stairs_dp.rs @@ -45,4 +45,4 @@ pub fn main() { let res = min_cost_climbing_stairs_dp_comp(&cost); println!("爬完楼梯的最低代价为 {res}"); -} \ No newline at end of file +} diff --git a/codes/rust/chapter_graph/graph_adjacency_list.rs b/codes/rust/chapter_graph/graph_adjacency_list.rs index 42b98d34b..b453c4e5d 100644 --- a/codes/rust/chapter_graph/graph_adjacency_list.rs +++ b/codes/rust/chapter_graph/graph_adjacency_list.rs @@ -139,4 +139,4 @@ fn main() { graph.remove_vertex(v[1]); println!("\n删除顶点 3 后,图为"); graph.print(); -} \ No newline at end of file +} diff --git a/codes/rust/chapter_graph/graph_adjacency_matrix.rs b/codes/rust/chapter_graph/graph_adjacency_matrix.rs index a94e20a5b..1418bee05 100644 --- a/codes/rust/chapter_graph/graph_adjacency_matrix.rs +++ b/codes/rust/chapter_graph/graph_adjacency_matrix.rs @@ -133,4 +133,4 @@ fn main() { graph.remove_vertex(1); println!("\n删除顶点 3 后,图为"); graph.print(); -} \ No newline at end of file +} diff --git a/codes/rust/chapter_graph/graph_bfs.rs b/codes/rust/chapter_graph/graph_bfs.rs index 32eb59eec..91525facc 100644 --- a/codes/rust/chapter_graph/graph_bfs.rs +++ b/codes/rust/chapter_graph/graph_bfs.rs @@ -66,4 +66,4 @@ fn main() { let res = graph_bfs(graph, v[0]); println!("\n广度优先遍历(BFS)顶点序列为"); println!("{:?}", vets_to_vals(res)); -} \ No newline at end of file +} diff --git a/codes/rust/chapter_graph/graph_dfs.rs b/codes/rust/chapter_graph/graph_dfs.rs index dc339e34c..374106669 100644 --- a/codes/rust/chapter_graph/graph_dfs.rs +++ b/codes/rust/chapter_graph/graph_dfs.rs @@ -58,4 +58,4 @@ fn main() { let res = graph_dfs(graph, v[0]); println!("\n深度优先遍历(DFS)顶点序列为"); println!("{:?}", vets_to_vals(res)); -} \ No newline at end of file +} diff --git a/codes/rust/chapter_greedy/coin_change_greedy.rs b/codes/rust/chapter_greedy/coin_change_greedy.rs index b4bfa63f2..bc2f8b891 100644 --- a/codes/rust/chapter_greedy/coin_change_greedy.rs +++ b/codes/rust/chapter_greedy/coin_change_greedy.rs @@ -51,4 +51,4 @@ fn main() { println!("\ncoins = {:?}, amt = {}", coins, amt); println!("凑到 {} 所需的最少硬币数量为 {}", amt, res); println!("实际上需要的最少数量为 2 ,即 49 + 49"); -} \ No newline at end of file +} diff --git a/codes/rust/chapter_greedy/fractional_knapsack.rs b/codes/rust/chapter_greedy/fractional_knapsack.rs index 13754ae4f..6e3ebe333 100644 --- a/codes/rust/chapter_greedy/fractional_knapsack.rs +++ b/codes/rust/chapter_greedy/fractional_knapsack.rs @@ -56,4 +56,4 @@ fn main() { // 贪心算法 let res = fractional_knapsack(&wgt, &val, cap); println!("不超过背包容量的最大物品价值为 {}", res); -} \ No newline at end of file +} diff --git a/codes/rust/chapter_greedy/max_capacity.rs b/codes/rust/chapter_greedy/max_capacity.rs index 4cfb17713..0d0ab348c 100644 --- a/codes/rust/chapter_greedy/max_capacity.rs +++ b/codes/rust/chapter_greedy/max_capacity.rs @@ -33,4 +33,4 @@ fn main() { // 贪心算法 let res = max_capacity(&ht); println!("最大容量为 {}", res); -} \ No newline at end of file +} diff --git a/codes/rust/chapter_hashing/array_hash_map.rs b/codes/rust/chapter_hashing/array_hash_map.rs index 6da399cbd..b77b5a68a 100644 --- a/codes/rust/chapter_hashing/array_hash_map.rs +++ b/codes/rust/chapter_hashing/array_hash_map.rs @@ -12,7 +12,9 @@ pub struct Pair { } /* 基于数组简易实现的哈希表 */ -pub struct ArrayHashMap { buckets: Vec> } +pub struct ArrayHashMap { + buckets: Vec> +} impl ArrayHashMap { pub fn new() -> ArrayHashMap { @@ -109,4 +111,4 @@ fn main() { for val in map.value_set() { println!("{}", val); } -} \ No newline at end of file +} diff --git a/codes/rust/chapter_heap/heap.rs b/codes/rust/chapter_heap/heap.rs index f81e127ca..4c0d9d9dc 100644 --- a/codes/rust/chapter_heap/heap.rs +++ b/codes/rust/chapter_heap/heap.rs @@ -70,4 +70,4 @@ fn main() { ); println!("\n输入列表并建立小顶堆后"); print_util::print_heap(min_heap.iter().map(|&val| min_heap_flag * val).collect()); -} \ No newline at end of file +} diff --git a/codes/rust/chapter_stack_and_queue/array_deque.rs b/codes/rust/chapter_stack_and_queue/array_deque.rs index 2b9e15244..7b0a0f0e2 100644 --- a/codes/rust/chapter_stack_and_queue/array_deque.rs +++ b/codes/rust/chapter_stack_and_queue/array_deque.rs @@ -116,6 +116,7 @@ impl ArrayDeque { } } +/* Driver Code */ fn main() { /* 初始化双向队列 */ let mut deque = ArrayDeque::new(10); diff --git a/codes/rust/chapter_stack_and_queue/array_queue.rs b/codes/rust/chapter_stack_and_queue/array_queue.rs index 171e66906..5481b1866 100644 --- a/codes/rust/chapter_stack_and_queue/array_queue.rs +++ b/codes/rust/chapter_stack_and_queue/array_queue.rs @@ -82,6 +82,7 @@ impl ArrayQueue { } } +/* Driver Code */ fn main() { /* 初始化队列 */ let capacity = 10;