mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-19 07:17:54 +08:00
feat: Revised the book (#978)
* Sync recent changes to the revised Word. * Revised the preface chapter * Revised the introduction chapter * Revised the computation complexity chapter * Revised the chapter data structure * Revised the chapter array and linked list * Revised the chapter stack and queue * Revised the chapter hashing * Revised the chapter tree * Revised the chapter heap * Revised the chapter graph * Revised the chapter searching * Reivised the sorting chapter * Revised the divide and conquer chapter * Revised the chapter backtacking * Revised the DP chapter * Revised the greedy chapter * Revised the appendix chapter * Revised the preface chapter doubly * Revised the figures
This commit is contained in:
@@ -35,11 +35,11 @@ fn insert(nums: &mut Vec<i32>, num: i32, index: usize) {
|
||||
for i in (index + 1..nums.len()).rev() {
|
||||
nums[i] = nums[i - 1];
|
||||
}
|
||||
// 将 num 赋给 index 处元素
|
||||
// 将 num 赋给 index 处的元素
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* 删除索引 index 处元素 */
|
||||
/* 删除索引 index 处的元素 */
|
||||
fn remove(nums: &mut Vec<i32>, index: usize) {
|
||||
// 把索引 index 之后的所有元素向前移动一位
|
||||
for i in index..nums.len() - 1 {
|
||||
|
||||
@@ -57,7 +57,7 @@ fn main() {
|
||||
let n2 = ListNode::new(2);
|
||||
let n3 = ListNode::new(5);
|
||||
let n4 = ListNode::new(4);
|
||||
// 构建引用指向
|
||||
// 构建节点之间的引用
|
||||
n0.borrow_mut().next = Some(n1.clone());
|
||||
n1.borrow_mut().next = Some(n2.clone());
|
||||
n2.borrow_mut().next = Some(n3.clone());
|
||||
|
||||
@@ -27,7 +27,7 @@ fn main() {
|
||||
print!("\n清空列表后 nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
// 尾部添加元素
|
||||
// 在尾部添加元素
|
||||
nums.push(1);
|
||||
nums.push(3);
|
||||
nums.push(2);
|
||||
@@ -36,7 +36,7 @@ fn main() {
|
||||
print!("\n添加元素后 nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
// 中间插入元素
|
||||
// 在中间插入元素
|
||||
nums.insert(3, 6);
|
||||
print!("\n在索引 3 处插入数字 6 ,得到 nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
@@ -6,12 +6,12 @@
|
||||
|
||||
include!("../include/include.rs");
|
||||
|
||||
/* 列表类简易实现 */
|
||||
/* 列表类 */
|
||||
#[allow(dead_code)]
|
||||
struct MyList {
|
||||
arr: Vec<i32>, // 数组(存储列表元素)
|
||||
capacity: usize, // 列表容量
|
||||
size: usize, // 列表长度(即当前元素数量)
|
||||
size: usize, // 列表长度(当前元素数量)
|
||||
extend_ratio: usize, // 每次列表扩容的倍数
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ impl MyList {
|
||||
}
|
||||
}
|
||||
|
||||
/* 获取列表长度(即当前元素数量)*/
|
||||
/* 获取列表长度(当前元素数量)*/
|
||||
pub fn size(&self) -> usize {
|
||||
return self.size;
|
||||
}
|
||||
@@ -52,7 +52,7 @@ impl MyList {
|
||||
self.arr[index] = num;
|
||||
}
|
||||
|
||||
/* 尾部添加元素 */
|
||||
/* 在尾部添加元素 */
|
||||
pub fn add(&mut self, num: i32) {
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if self.size == self.capacity() {
|
||||
@@ -63,7 +63,7 @@ impl MyList {
|
||||
self.size += 1;
|
||||
}
|
||||
|
||||
/* 中间插入元素 */
|
||||
/* 在中间插入元素 */
|
||||
pub fn insert(&mut self, index: usize, num: i32) {
|
||||
if index >= self.size() {panic!("索引越界")};
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
@@ -117,7 +117,7 @@ impl MyList {
|
||||
fn main() {
|
||||
/* 初始化列表 */
|
||||
let mut nums = MyList::new(10);
|
||||
/* 尾部添加元素 */
|
||||
/* 在尾部添加元素 */
|
||||
nums.add(1);
|
||||
nums.add(3);
|
||||
nums.add(2);
|
||||
@@ -127,7 +127,7 @@ fn main() {
|
||||
print_util::print_array(&nums.to_array());
|
||||
print!(" ,容量 = {} ,长度 = {}", nums.capacity(), nums.size());
|
||||
|
||||
/* 中间插入元素 */
|
||||
/* 在中间插入元素 */
|
||||
nums.insert(3, 6);
|
||||
print!("\n在索引 3 处插入数字 6 ,得到 nums = ");
|
||||
print_util::print_array(&nums.to_array());
|
||||
|
||||
Reference in New Issue
Block a user