Finetune Rust code.

This commit is contained in:
krahets 2023-07-26 10:01:49 +08:00
parent 06006c58a2
commit 60162f6fa8
18 changed files with 29 additions and 25 deletions

View File

@ -7,6 +7,7 @@
include!("../include/include.rs"); include!("../include/include.rs");
#[allow(dead_code)] #[allow(dead_code)]
/* 列表类简易实现 */
struct MyList { struct MyList {
nums: Vec<i32>, // 数组(存储列表元素) nums: Vec<i32>, // 数组(存储列表元素)
capacity: usize, // 列表容量 capacity: usize, // 列表容量

View File

@ -4,10 +4,10 @@
* Author: sjinzh (sjinzh@gmail.com) * Author: sjinzh (sjinzh@gmail.com)
*/ */
use std::{cell::RefCell, rc::Rc}; use std::{cell::RefCell, rc::Rc};
use std::collections::HashMap; use std::collections::HashMap;
include!("../include/include.rs"); include!("../include/include.rs");
use tree_node::TreeNode; use tree_node::TreeNode;
/* 构建二叉树:分治 */ /* 构建二叉树:分治 */
fn dfs(preorder: &[i32], inorder: &[i32], hmap: &HashMap<i32, i32>, i: i32, l: i32, r: i32) -> Option<Rc<RefCell<TreeNode>>> { fn dfs(preorder: &[i32], inorder: &[i32], hmap: &HashMap<i32, i32>, i: i32, l: i32, r: i32) -> Option<Rc<RefCell<TreeNode>>> {
@ -26,7 +26,7 @@ fn dfs(preorder: &[i32], inorder: &[i32], hmap: &HashMap<i32, i32>, i: i32, l: i
} }
/* 构建二叉树 */ /* 构建二叉树 */
fn build_tree(preorder: &[i32], inorder: &[i32]) -> Option<Rc<RefCell<TreeNode>>> { fn build_tree(preorder: &[i32], inorder: &[i32]) -> Option<Rc<RefCell<TreeNode>>> {
// 初始化哈希表,存储 inorder 元素到索引的映射 // 初始化哈希表,存储 inorder 元素到索引的映射
let mut hmap: HashMap<i32, i32> = HashMap::new(); let mut hmap: HashMap<i32, i32> = HashMap::new();
for i in 0..inorder.len() { for i in 0..inorder.len() {
@ -36,8 +36,8 @@ fn dfs(preorder: &[i32], inorder: &[i32], hmap: &HashMap<i32, i32>, i: i32, l: i
root root
} }
/* Driver Code */ /* Driver Code */
fn main() { fn main() {
let preorder = [ 3, 9, 2, 1, 7 ]; let preorder = [ 3, 9, 2, 1, 7 ];
let inorder = [ 9, 3, 1, 2, 7 ]; let inorder = [ 9, 3, 1, 2, 7 ];
println!("中序遍历 = {:?}", preorder); println!("中序遍历 = {:?}", preorder);
@ -46,5 +46,4 @@ fn dfs(preorder: &[i32], inorder: &[i32], hmap: &HashMap<i32, i32>, i: i32, l: i
let root = build_tree(&preorder, &inorder); let root = build_tree(&preorder, &inorder);
println!("构建的二叉树为:"); println!("构建的二叉树为:");
print_util::print_tree(root.as_ref().unwrap()); print_util::print_tree(root.as_ref().unwrap());
} }

View File

@ -12,7 +12,9 @@ pub struct Pair {
} }
/* 基于数组简易实现的哈希表 */ /* 基于数组简易实现的哈希表 */
pub struct ArrayHashMap { buckets: Vec<Option<Pair>> } pub struct ArrayHashMap {
buckets: Vec<Option<Pair>>
}
impl ArrayHashMap { impl ArrayHashMap {
pub fn new() -> ArrayHashMap { pub fn new() -> ArrayHashMap {

View File

@ -116,6 +116,7 @@ impl ArrayDeque {
} }
} }
/* Driver Code */
fn main() { fn main() {
/* 初始化双向队列 */ /* 初始化双向队列 */
let mut deque = ArrayDeque::new(10); let mut deque = ArrayDeque::new(10);

View File

@ -82,6 +82,7 @@ impl ArrayQueue {
} }
} }
/* Driver Code */
fn main() { fn main() {
/* 初始化队列 */ /* 初始化队列 */
let capacity = 10; let capacity = 10;