diff --git a/chapter_array_and_linkedlist/array.md b/chapter_array_and_linkedlist/array.md index 67fee24c3..c7e95397f 100755 --- a/chapter_array_and_linkedlist/array.md +++ b/chapter_array_and_linkedlist/array.md @@ -106,6 +106,7 @@ comments: true === "C" ```c title="array.c" + /* 初始化数组 */ int arr[5] = { 0 }; // { 0, 0, 0, 0, 0 } int nums[5] = { 1, 3, 2, 5, 4 }; ``` diff --git a/chapter_backtracking/backtracking_algorithm.md b/chapter_backtracking/backtracking_algorithm.md index 7511aed4a..b7e0fa514 100644 --- a/chapter_backtracking/backtracking_algorithm.md +++ b/chapter_backtracking/backtracking_algorithm.md @@ -1020,7 +1020,27 @@ comments: true === "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/chapter_data_structure/basic_data_types.md b/chapter_data_structure/basic_data_types.md index 914c4e13f..feca29739 100644 --- a/chapter_data_structure/basic_data_types.md +++ b/chapter_data_structure/basic_data_types.md @@ -143,7 +143,11 @@ comments: true === "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/chapter_hashing/hash_algorithm.md b/chapter_hashing/hash_algorithm.md index 759141a40..4262878ba 100644 --- a/chapter_hashing/hash_algorithm.md +++ b/chapter_hashing/hash_algorithm.md @@ -816,7 +816,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 ``` === "C" diff --git a/chapter_hashing/hash_collision.md b/chapter_hashing/hash_collision.md index 5e57a1133..68cc3566b 100644 --- a/chapter_hashing/hash_collision.md +++ b/chapter_hashing/hash_collision.md @@ -1365,7 +1365,7 @@ comments: true 为此,考虑在线性探测中记录遇到的首个 `TOMBSTONE` 的索引,并将搜索到的目标元素与该 `TOMBSTONE` 交换位置。这样做的好处是当每次查询或添加元素时,元素会被移动至距离理想位置(探测起始点)更近的桶,从而优化查询效率。 -以下代码实现了一个包含懒删除的开放寻址(线性探测)哈希表。为了更加充分地使用哈希表的空间,我们将哈希表表看作是一个“环形数组”,当越过数组尾部时,回到头部继续遍历。 +以下代码实现了一个包含懒删除的开放寻址(线性探测)哈希表。为了更加充分地使用哈希表的空间,我们将哈希表看作是一个“环形数组”,当越过数组尾部时,回到头部继续遍历。 === "Python" diff --git a/chapter_hashing/hash_map.md b/chapter_hashing/hash_map.md index f96faae5c..362c64211 100755 --- a/chapter_hashing/hash_map.md +++ b/chapter_hashing/hash_map.md @@ -130,7 +130,7 @@ comments: true === "Go" - ```go title="hash_map.go" + ```go title="hash_map_test.go" /* 初始化哈希表 */ hmap := make(map[int]string) diff --git a/chapter_heap/heap.md b/chapter_heap/heap.md index ef7e30d56..4dfca5035 100644 --- a/chapter_heap/heap.md +++ b/chapter_heap/heap.md @@ -306,7 +306,41 @@ comments: true === "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/chapter_preface/suggestions.md b/chapter_preface/suggestions.md index c84c95acb..bec3065fe 100644 --- a/chapter_preface/suggestions.md +++ b/chapter_preface/suggestions.md @@ -197,7 +197,7 @@ comments: true **第一步:安装本地编程环境**。请参照[附录教程](https://www.hello-algo.com/chapter_appendix/installation/)进行安装,如果已安装则可跳过此步骤。 -**第二步:下载代码仓**。如果已经安装 [Git](https://git-scm.com/downloads) ,可以通过以下命令克隆本仓库。 +**第二步:克隆或下载代码仓**。如果已经安装 [Git](https://git-scm.com/downloads) ,可以通过以下命令克隆本仓库。 ```shell git clone https://github.com/krahets/hello-algo.git diff --git a/chapter_stack_and_queue/stack.md b/chapter_stack_and_queue/stack.md index 30343fa01..7261fed97 100755 --- a/chapter_stack_and_queue/stack.md +++ b/chapter_stack_and_queue/stack.md @@ -288,20 +288,16 @@ comments: true 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/chapter_tree/array_representation_of_tree.md b/chapter_tree/array_representation_of_tree.md index 3bed9ff53..a3252e68f 100644 --- a/chapter_tree/array_representation_of_tree.md +++ b/chapter_tree/array_representation_of_tree.md @@ -107,7 +107,9 @@ comments: true === "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/chapter_tree/avl_tree.md b/chapter_tree/avl_tree.md index 80b745cb0..8ba5c3e13 100644 --- a/chapter_tree/avl_tree.md +++ b/chapter_tree/avl_tree.md @@ -161,7 +161,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 { + /* 构造方法 */ + fn new(val: i32) -> Rc> { + Rc::new(RefCell::new(Self { + val, + height: 0, + left: None, + right: None + })) + } + } ``` === "C" diff --git a/chapter_tree/binary_tree.md b/chapter_tree/binary_tree.md index c74d21556..5bd5f8778 100644 --- a/chapter_tree/binary_tree.md +++ b/chapter_tree/binary_tree.md @@ -62,7 +62,7 @@ comments: true Left *TreeNode Right *TreeNode } - /* 节点初始化方法 */ + /* 构造方法 */ func NewTreeNode(v int) *TreeNode { return &TreeNode{ Left: nil, // 左子节点指针 @@ -135,7 +135,26 @@ comments: true === "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" @@ -359,7 +378,17 @@ comments: true === "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" @@ -502,7 +531,12 @@ comments: true === "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"