diff --git a/docs/chapter_backtracking/backtracking_algorithm.md b/docs/chapter_backtracking/backtracking_algorithm.md index 4aba5ac34..d0e45f7e0 100644 --- a/docs/chapter_backtracking/backtracking_algorithm.md +++ b/docs/chapter_backtracking/backtracking_algorithm.md @@ -532,7 +532,27 @@ === "Rust" ```rust title="" - + /* 回溯算法框架 */ + fn backtrack(state: &mut State, choices: &Vec, res: &mut Vec) { + // 判断是否为解 + if is_solution(state) { + // 记录解 + record_solution(state, res); + // 停止继续搜索 + return; + } + // 遍历所有选择 + for choice in choices { + // 剪枝:判断选择是否合法 + if is_valid(state, choice) { + // 尝试:做出选择,更新状态 + make_choice(state, choice); + backtrack(state, choices, res); + // 回退:撤销选择,恢复到之前的状态 + undo_choice(state, choice); + } + } + } ``` === "C" diff --git a/docs/chapter_data_structure/basic_data_types.md b/docs/chapter_data_structure/basic_data_types.md index 3c83da125..32aa42e25 100644 --- a/docs/chapter_data_structure/basic_data_types.md +++ b/docs/chapter_data_structure/basic_data_types.md @@ -135,7 +135,11 @@ === "Rust" ```rust title="" - + // 使用多种基本数据类型来初始化数组 + let numbers: Vec = vec![0; 5]; + let decimals: Vec = vec![0.0, 5]; + let characters: Vec = vec!['0'; 5]; + let bools: Vec = vec![false; 5]; ``` === "C" diff --git a/docs/chapter_hashing/hash_algorithm.md b/docs/chapter_hashing/hash_algorithm.md index 38acf2b8f..e9f8bb2b8 100644 --- a/docs/chapter_hashing/hash_algorithm.md +++ b/docs/chapter_hashing/hash_algorithm.md @@ -436,6 +436,45 @@ $$ === "Rust" ```rust title="built_in_hash.rs" + use std::collections::hash_map::DefaultHasher; + use std::hash::{Hash, Hasher}; + + let num = 3; + let mut num_hasher = DefaultHasher::new(); + num.hash(&mut num_hasher); + let hash_num = num_hasher.finish(); + // 整数 3 的哈希值为 568126464209439262 + + let bol = true; + let mut bol_hasher = DefaultHasher::new(); + bol.hash(&mut bol_hasher); + let hash_bol = bol_hasher.finish(); + // 布尔量 true 的哈希值为 4952851536318644461 + + let dec: f32 = 3.14159; + let mut dec_hasher = DefaultHasher::new(); + dec.to_bits().hash(&mut dec_hasher); + let hash_dec = dec_hasher.finish(); + println!("小数 {} 的哈希值为 {}", dec, hash_dec); + // 小数 3.14159 的哈希值为 2566941990314602357 + + let str = "Hello 算法"; + let mut str_hasher = DefaultHasher::new(); + str.hash(&mut str_hasher); + let hash_str = str_hasher.finish(); + // 字符串 Hello 算法 的哈希值为 16092673739211250988 + + let arr = (&12836, &"小哈"); + let mut tup_hasher = DefaultHasher::new(); + arr.hash(&mut tup_hasher); + let hash_tup = tup_hasher.finish(); + // 元组 (12836, "小哈") 的哈希值为 1885128010422702749 + + let node = ListNode::new(42); + let mut hasher = DefaultHasher::new(); + node.borrow().val.hash(&mut hasher); + let hash = hasher.finish(); + // 节点对象 RefCell { value: ListNode { val: 42, next: None } } 的哈希值为15387811073369036852 ``` diff --git a/docs/chapter_heap/heap.md b/docs/chapter_heap/heap.md index 6c5c831e7..f39a36adc 100644 --- a/docs/chapter_heap/heap.md +++ b/docs/chapter_heap/heap.md @@ -296,7 +296,41 @@ === "Rust" ```rust title="heap.rs" + use std::collections::BinaryHeap; + use std::cmp::Reverse; + /* 初始化堆 */ + // 初始化小顶堆 + let mut min_heap = BinaryHeap::>::new(); + // 初始化大顶堆 + let mut max_heap = BinaryHeap::new(); + + /* 元素入堆 */ + max_heap.push(1); + max_heap.push(3); + max_heap.push(2); + max_heap.push(5); + max_heap.push(4); + + /* 获取堆顶元素 */ + let peek = max_heap.peek().unwrap(); // 5 + + /* 堆顶元素出堆 */ + // 出堆元素会形成一个从大到小的序列 + let peek = max_heap.pop().unwrap(); // 5 + let peek = max_heap.pop().unwrap(); // 4 + let peek = max_heap.pop().unwrap(); // 3 + let peek = max_heap.pop().unwrap(); // 2 + let peek = max_heap.pop().unwrap(); // 1 + + /* 获取堆大小 */ + let size = max_heap.len(); + + /* 判断堆是否为空 */ + let is_empty = max_heap.is_empty(); + + /* 输入列表并建堆 */ + let min_heap = BinaryHeap::from(vec![Reverse(1), Reverse(3), Reverse(2), Reverse(5), Reverse(4)]); ``` === "C" diff --git a/docs/chapter_stack_and_queue/stack.md b/docs/chapter_stack_and_queue/stack.md index cf6656eb2..f2738f75b 100755 --- a/docs/chapter_stack_and_queue/stack.md +++ b/docs/chapter_stack_and_queue/stack.md @@ -278,20 +278,16 @@ stack.push(4); /* 访问栈顶元素 */ - if let Some(top) = stack.get(stack.len() - 1) { - } - if let Some(top) = stack.last() { - } + let top = stack[stack.len() - 1]; /* 元素出栈 */ - if let Some(pop) = stack.pop() { - } + let pop = stack.pop().unwrap(); /* 获取栈的长度 */ let size = stack.len(); /* 判断是否为空 */ - let isEmpty = stack.is_empty(); + let is_empty = stack.is_empty(); ``` === "C" diff --git a/docs/chapter_tree/array_representation_of_tree.md b/docs/chapter_tree/array_representation_of_tree.md index df4086e11..453f5fb51 100644 --- a/docs/chapter_tree/array_representation_of_tree.md +++ b/docs/chapter_tree/array_representation_of_tree.md @@ -99,7 +99,9 @@ === "Rust" ```rust title="" - + /* 二叉树的数组表示 */ + // 使用 None 来标记空位 + let tree = [Some(1), Some(2), Some(3), Some(4), None, Some(6), Some(7), Some(8), Some(9), None, None, Some(12), None, None, Some(15)]; ``` === "C" diff --git a/docs/chapter_tree/avl_tree.md b/docs/chapter_tree/avl_tree.md index af318367e..89472f22c 100644 --- a/docs/chapter_tree/avl_tree.md +++ b/docs/chapter_tree/avl_tree.md @@ -153,7 +153,28 @@ AVL 树既是二叉搜索树也是平衡二叉树,同时满足这两类二叉 === "Rust" ```rust title="" + use std::rc::Rc; + use std::cell::RefCell; + /* AVL 树节点类型 */ + struct TreeNode { + val: i32, // 节点值 + height: i32, // 节点高度 + left: Option>>, // 左子节点 + right: Option>>, // 右子节点 + } + + impl TreeNode { + /* AVL 树节点构造方法 */ + fn new(val: i32) -> Rc> { + Rc::new(RefCell::new(Self { + val, + height: 0, + left: None, + right: None + })) + } + } ``` === "C" diff --git a/docs/chapter_tree/binary_tree.md b/docs/chapter_tree/binary_tree.md index 7fc872c80..8a90e3df8 100644 --- a/docs/chapter_tree/binary_tree.md +++ b/docs/chapter_tree/binary_tree.md @@ -126,7 +126,26 @@ === "Rust" ```rust title="" + use std::rc::Rc; + use std::cell::RefCell; + /* 二叉树节点类型 */ + struct TreeNode { + val: i32, // 节点值 + left: Option>>, // 左子节点引用 + right: Option>>, // 右子节点引用 + } + + impl TreeNode { + /* 二叉树节点构造方法 */ + fn new(val: i32) -> Rc> { + Rc::new(RefCell::new(Self { + val, + left: None, + right: None + })) + } + } ``` === "C" @@ -346,7 +365,17 @@ === "Rust" ```rust title="binary_tree.rs" - + // 初始化节点 + let n1 = TreeNode::new(1); + let n2 = TreeNode::new(2); + let n3 = TreeNode::new(3); + let n4 = TreeNode::new(4); + let n5 = TreeNode::new(5); + // 构建引用指向(即指针) + n1.borrow_mut().left = Some(n2.clone()); + n1.borrow_mut().right = Some(n3); + n2.borrow_mut().left = Some(n4); + n2.borrow_mut().right = Some(n5); ``` === "C" @@ -487,7 +516,12 @@ === "Rust" ```rust title="binary_tree.rs" - + let p = TreeNode::new(0); + // 在 n1 -> n2 中间插入节点 P + n1.borrow_mut().left = Some(p.clone()); + p.borrow_mut().left = Some(n2.clone()); + // 删除节点 p + n1.borrow_mut().left = Some(n2); ``` === "C"