Finetune Rust code.

pull/656/head
krahets 1 year ago
parent 06006c58a2
commit 60162f6fa8

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

@ -4,10 +4,10 @@
* 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<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 元素到索引的映射
let mut hmap: HashMap<i32, i32> = HashMap::new();
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
}
/* 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<i32, i32>, i: i32, l: i
let root = build_tree(&preorder, &inorder);
println!("构建的二叉树为:");
print_util::print_tree(root.as_ref().unwrap());
}
}

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

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

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

Loading…
Cancel
Save