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,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<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());
}
}

@ -34,4 +34,4 @@ pub fn main() {
let res = climbing_stairs_backtrack(n);
println!("爬 {n} 阶楼梯共有 {res} 种方案");
}
}

@ -28,4 +28,4 @@ pub fn main() {
let res = climbing_stairs_constraint_dp(n);
println!("爬 {n} 阶楼梯共有 {res} 种方案");
}
}

@ -24,4 +24,4 @@ pub fn main() {
let res = climbing_stairs_dfs(n);
println!("爬 {n} 阶楼梯共有 {res} 种方案");
}
}

@ -41,4 +41,4 @@ pub fn main() {
let res = climbing_stairs_dp_comp(n);
println!("爬 {n} 阶楼梯共有 {res} 种方案");
}
}

@ -45,4 +45,4 @@ pub fn main() {
let res = min_cost_climbing_stairs_dp_comp(&cost);
println!("爬完楼梯的最低代价为 {res}");
}
}

@ -139,4 +139,4 @@ fn main() {
graph.remove_vertex(v[1]);
println!("\n删除顶点 3 后,图为");
graph.print();
}
}

@ -133,4 +133,4 @@ fn main() {
graph.remove_vertex(1);
println!("\n删除顶点 3 后,图为");
graph.print();
}
}

@ -66,4 +66,4 @@ fn main() {
let res = graph_bfs(graph, v[0]);
println!("\n广度优先遍历BFS顶点序列为");
println!("{:?}", vets_to_vals(res));
}
}

@ -58,4 +58,4 @@ fn main() {
let res = graph_dfs(graph, v[0]);
println!("\n深度优先遍历DFS顶点序列为");
println!("{:?}", vets_to_vals(res));
}
}

@ -51,4 +51,4 @@ fn main() {
println!("\ncoins = {:?}, amt = {}", coins, amt);
println!("凑到 {} 所需的最少硬币数量为 {}", amt, res);
println!("实际上需要的最少数量为 2 ,即 49 + 49");
}
}

@ -56,4 +56,4 @@ fn main() {
// 贪心算法
let res = fractional_knapsack(&wgt, &val, cap);
println!("不超过背包容量的最大物品价值为 {}", res);
}
}

@ -33,4 +33,4 @@ fn main() {
// 贪心算法
let res = max_capacity(&ht);
println!("最大容量为 {}", res);
}
}

@ -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 {
@ -109,4 +111,4 @@ fn main() {
for val in map.value_set() {
println!("{}", val);
}
}
}

@ -70,4 +70,4 @@ fn main() {
);
println!("\n输入列表并建立小顶堆后");
print_util::print_heap(min_heap.iter().map(|&val| min_heap_flag * val).collect());
}
}

@ -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