This commit is contained in:
krahets
2023-08-08 23:15:13 +08:00
parent 48adefba25
commit 6d41b5da04
11 changed files with 350 additions and 150 deletions

View File

@@ -109,7 +109,9 @@ comments: true
=== "Rust"
```rust title="array.rs"
/* 初始化数组 */
let arr: Vec<i32> = vec![0; 5]; // [0, 0, 0, 0, 0]
let nums: Vec<i32> = vec![1, 3, 2, 5, 4];
```
## 4.1.1. &nbsp; 数组优点

View File

@@ -172,7 +172,14 @@ comments: true
=== "Rust"
```rust title=""
use std::rc::Rc;
use std::cell::RefCell;
/* 链表节点类 */
#[derive(Debug)]
struct ListNode {
val: i32, // 节点值
next: Option<Rc<RefCell<ListNode>>>, // 指向下一节点的指针(引用)
}
```
我们将链表的首个节点称为「头节点」,最后一个节点称为「尾节点」。尾节点指向的是“空”,在 Java, C++, Python 中分别记为 $\text{null}$ , $\text{nullptr}$ , $\text{None}$ 。在不引起歧义的前提下,本书都使用 $\text{None}$ 来表示空。
@@ -369,7 +376,19 @@ comments: true
=== "Rust"
```rust title="linked_list.rs"
/* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */
// 初始化各个节点
let n0 = Rc::new(RefCell::new(ListNode { val: 1, next: None }));
let n1 = Rc::new(RefCell::new(ListNode { val: 3, next: None }));
let n2 = Rc::new(RefCell::new(ListNode { val: 2, next: None }));
let n3 = Rc::new(RefCell::new(ListNode { val: 5, next: None }));
let n4 = Rc::new(RefCell::new(ListNode { val: 4, next: None }));
// 构建引用指向
n0.borrow_mut().next = Some(n1.clone());
n1.borrow_mut().next = Some(n2.clone());
n2.borrow_mut().next = Some(n3.clone());
n3.borrow_mut().next = Some(n4.clone());
```
在编程语言中,数组整体是一个变量,比如数组 `nums` 包含元素 `nums[0]` , `nums[1]` 等。而链表是由多个分散的节点对象组成,**我们通常将头节点当作链表的代称**,比如以上代码中的链表可被记做链表 `n0` 。
@@ -1249,7 +1268,27 @@ comments: true
=== "Rust"
```rust title=""
use std::rc::Rc;
use std::cell::RefCell;
/* 双向链表节点类型 */
#[derive(Debug)]
struct ListNode {
val: i32, // 节点值
next: Option<Rc<RefCell<ListNode>>>, // 指向后继节点的指针(引用)
prev: Option<Rc<RefCell<ListNode>>>, // 指向前驱节点的指针(引用)
}
/* 构造函数 */
impl ListNode {
fn new(val: i32) -> Self {
ListNode {
val,
next: None,
prev: None,
}
}
}
```
![常见链表种类](linked_list.assets/linkedlist_common_types.png)

View File

@@ -123,7 +123,11 @@ comments: true
=== "Rust"
```rust title="list.rs"
/* 初始化列表 */
// 无初始值
let list1: Vec<i32> = Vec::new();
// 有初始值
let list2: Vec<i32> = vec![1, 3, 2, 5, 4];
```
**访问与更新元素**。由于列表的底层数据结构是数组,因此可以在 $O(1)$ 时间内访问和更新元素,效率很高。
@@ -237,7 +241,10 @@ comments: true
=== "Rust"
```rust title="list.rs"
/* 访问元素 */
let num: i32 = list[1]; // 访问索引 1 处的元素
/* 更新元素 */
list[1] = 0; // 将索引 1 处的元素更新为 0
```
**在列表中添加、插入、删除元素**。相较于数组,列表可以自由地添加与删除元素。在列表尾部添加元素的时间复杂度为 $O(1)$ ,但插入和删除元素的效率仍与数组相同,时间复杂度为 $O(N)$ 。
@@ -451,7 +458,21 @@ comments: true
=== "Rust"
```rust title="list.rs"
/* 清空列表 */
list.clear();
/* 尾部添加元素 */
list.push(1);
list.push(3);
list.push(2);
list.push(5);
list.push(4);
/* 中间插入元素 */
list.insert(3, 6); // 在索引 3 处插入数字 6
/* 删除元素 */
list.remove(3); // 删除索引 3 处的元素
```
**遍历列表**。与数组一样,列表可以根据索引遍历,也可以直接遍历各元素。
@@ -624,7 +645,17 @@ comments: true
=== "Rust"
```rust title="list.rs"
/* 通过索引遍历列表 */
let mut count = 0;
for (index, value) in list.iter().enumerate() {
count += 1;
}
/* 直接遍历列表元素 */
let mut count = 0;
for value in list.iter() {
count += 1;
}
```
**拼接两个列表**。给定一个新列表 `list1` ,我们可以将该列表拼接到原列表的尾部。
@@ -721,7 +752,9 @@ comments: true
=== "Rust"
```rust title="list.rs"
/* 拼接两个列表 */
let list1: Vec<i32> = vec![6, 8, 7, 10, 9];
list.extend(list1);
```
**排序列表**。排序也是常用的方法之一。完成列表排序后,我们便可以使用在数组类算法题中经常考察的「二分查找」和「双指针」算法。
@@ -805,7 +838,8 @@ comments: true
=== "Rust"
```rust title="list.rs"
/* 排序列表 */
list.sort(); // 排序后,列表元素从小到大排列
```
## 4.3.2. &nbsp; 列表实现 *