This commit is contained in:
krahets
2023-08-20 13:37:08 +08:00
parent 2e27ad1680
commit 44a8568356
32 changed files with 140 additions and 130 deletions
+6 -6
View File
@@ -10,7 +10,7 @@ comments: true
## 4.3.1   列表常用操作
### 初始化列表
### 1.   初始化列表
我们通常使用“无初始值”和“有初始值”这两种初始化方法。
@@ -132,7 +132,7 @@ comments: true
let list2: Vec<i32> = vec![1, 3, 2, 5, 4];
```
### 访问元素
### 2. &nbsp; 访问元素
列表本质上是数组,因此可以在 $O(1)$ 时间内访问和更新元素,效率很高。
@@ -251,7 +251,7 @@ comments: true
list[1] = 0; // 将索引 1 处的元素更新为 0
```
### 插入与删除元素
### 3. &nbsp; 插入与删除元素
相较于数组,列表可以自由地添加与删除元素。在列表尾部添加元素的时间复杂度为 $O(1)$ ,但插入和删除元素的效率仍与数组相同,时间复杂度为 $O(n)$ 。
@@ -481,7 +481,7 @@ comments: true
list.remove(3); // 删除索引 3 处的元素
```
### 遍历列表
### 4. &nbsp; 遍历列表
与数组一样,列表可以根据索引遍历,也可以直接遍历各元素。
@@ -666,7 +666,7 @@ comments: true
}
```
### 拼接列表
### 5. &nbsp; 拼接列表
给定一个新列表 `list1` ,我们可以将该列表拼接到原列表的尾部。
@@ -767,7 +767,7 @@ comments: true
list.extend(list1);
```
### 排序列表
### 6. &nbsp; 排序列表
完成列表排序后,我们便可以使用在数组类算法题中经常考察的“二分查找”和“双指针”算法。