2022-11-22 17:47:26 +08:00
|
|
|
|
---
|
|
|
|
|
comments: true
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
# 数组
|
|
|
|
|
|
|
|
|
|
「数组 Array」是一种将 **相同类型元素** 存储在 **连续内存空间** 的数据结构,将元素在数组中的位置称为元素的「索引 Index」。
|
|
|
|
|
|
|
|
|
|
![array_definition](array.assets/array_definition.png)
|
|
|
|
|
|
2022-11-23 15:50:59 +08:00
|
|
|
|
<p align="center"> Fig. 数组定义与存储方式 </p>
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
!!! note
|
|
|
|
|
|
|
|
|
|
观察上图,我们发现 **数组首元素的索引为 $0$** 。你可能会想,这并不符合日常习惯,首个元素的索引为什么不是 $1$ 呢,这不是更加自然吗?我认同你的想法,但请先记住这个设定,后面讲内存地址计算时,我会尝试解答这个问题。
|
|
|
|
|
|
2023-01-07 20:34:32 +08:00
|
|
|
|
**数组有多种初始化写法**。根据实际需要,选代码最短的那一种就好。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="array.java"
|
|
|
|
|
/* 初始化数组 */
|
|
|
|
|
int[] arr = new int[5]; // { 0, 0, 0, 0, 0 }
|
|
|
|
|
int[] nums = { 1, 3, 2, 5, 4 };
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="array.cpp"
|
2022-11-27 19:07:35 +08:00
|
|
|
|
/* 初始化数组 */
|
|
|
|
|
int* arr = new int[5];
|
|
|
|
|
int* nums = new int[5] { 1, 3, 2, 5, 4 };
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="array.py"
|
2022-11-25 03:59:38 +08:00
|
|
|
|
""" 初始化数组 """
|
|
|
|
|
arr = [0] * 5 # [ 0, 0, 0, 0, 0 ]
|
|
|
|
|
nums = [1, 3, 2, 5, 4]
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="array.go"
|
2022-12-27 11:25:30 +08:00
|
|
|
|
/* 初始化数组 */
|
2023-01-06 03:28:27 +08:00
|
|
|
|
var arr [5]int
|
|
|
|
|
// 在 Go 中,指定长度时([5]int)为数组,不指定长度时([]int)为切片
|
|
|
|
|
// 由于 Go 的数组被设计为在编译期确定长度,因此只能使用常量来指定长度
|
|
|
|
|
// 为了方便实现扩容 extend() 方法,以下将切片(Slice)看作数组(Array)
|
2022-12-28 10:46:12 +08:00
|
|
|
|
nums := []int{1, 3, 2, 5, 4}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```javascript title="array.js"
|
|
|
|
|
/* 初始化数组 */
|
2022-12-12 21:39:48 +08:00
|
|
|
|
var arr = new Array(5).fill(0);
|
|
|
|
|
var nums = [1, 3, 2, 5, 4];
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="array.ts"
|
2022-12-04 18:00:44 +08:00
|
|
|
|
/* 初始化数组 */
|
2022-12-12 21:25:07 +08:00
|
|
|
|
let arr: number[] = new Array(5).fill(0);
|
|
|
|
|
let nums: number[] = [1, 3, 2, 5, 4];
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="array.c"
|
2022-12-09 21:47:24 +08:00
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="array.cs"
|
2022-12-14 15:11:25 +08:00
|
|
|
|
/* 初始化数组 */
|
2022-12-14 11:03:28 +08:00
|
|
|
|
int[] arr = new int[5]; // { 0, 0, 0, 0, 0 }
|
|
|
|
|
int[] nums = { 1, 3, 2, 5, 4 };
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-01-05 21:16:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="array.swift"
|
2023-01-08 20:53:24 +08:00
|
|
|
|
/* 初始化数组 */
|
2023-01-05 21:16:05 +08:00
|
|
|
|
let arr = Array(repeating: 0, count: 5) // [0, 0, 0, 0, 0]
|
|
|
|
|
let nums = [1, 3, 2, 5, 4]
|
|
|
|
|
```
|
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
## 数组优点
|
|
|
|
|
|
2023-01-07 20:34:32 +08:00
|
|
|
|
**在数组中访问元素非常高效**。这是因为在数组中,计算元素的内存地址非常容易。给定数组首个元素的地址、和一个元素的索引,利用以下公式可以直接计算得到该元素的内存地址,从而直接访问此元素。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
![array_memory_location_calculation](array.assets/array_memory_location_calculation.png)
|
|
|
|
|
|
2022-11-23 15:50:59 +08:00
|
|
|
|
<p align="center"> Fig. 数组元素的内存地址计算 </p>
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
```java title=""
|
|
|
|
|
// 元素内存地址 = 数组内存地址 + 元素长度 * 元素索引
|
|
|
|
|
elementAddr = firtstElementAddr + elementLength * elementIndex
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**为什么数组元素索引从 0 开始编号?** 根据地址计算公式,**索引本质上表示的是内存地址偏移量**,首个元素的地址偏移量是 $0$ ,那么索引是 $0$ 也就很自然了。
|
|
|
|
|
|
|
|
|
|
访问元素的高效性带来了许多便利。例如,我们可以在 $O(1)$ 时间内随机获取一个数组中的元素。
|
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="array.java"
|
|
|
|
|
/* 随机返回一个数组元素 */
|
|
|
|
|
int randomAccess(int[] nums) {
|
|
|
|
|
int randomIndex = ThreadLocalRandom.current().
|
|
|
|
|
nextInt(0, nums.length);
|
|
|
|
|
int randomNum = nums[randomIndex];
|
|
|
|
|
return randomNum;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="array.cpp"
|
2022-11-27 19:07:35 +08:00
|
|
|
|
/* 随机返回一个数组元素 */
|
|
|
|
|
int randomAccess(int* nums, int size) {
|
|
|
|
|
// 在区间 [0, size) 中随机抽取一个数字
|
|
|
|
|
int randomIndex = rand() % size;
|
|
|
|
|
// 获取并返回随机元素
|
|
|
|
|
int randomNum = nums[randomIndex];
|
|
|
|
|
return randomNum;
|
|
|
|
|
}
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="array.py"
|
2022-11-25 03:59:38 +08:00
|
|
|
|
""" 随机访问元素 """
|
|
|
|
|
def randomAccess(nums):
|
|
|
|
|
# 在区间 [0, len(nums)) 中随机抽取一个数字
|
|
|
|
|
random_index = random.randint(0, len(nums))
|
|
|
|
|
# 获取并返回随机元素
|
|
|
|
|
random_num = nums[random_index]
|
|
|
|
|
return random_num
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="array.go"
|
2022-12-29 13:26:09 +08:00
|
|
|
|
/* 随机返回一个数组元素 */
|
2023-01-02 19:03:36 +08:00
|
|
|
|
func randomAccess(nums []int) (randomNum int) {
|
2022-12-27 11:25:30 +08:00
|
|
|
|
// 在区间 [0, nums.length) 中随机抽取一个数字
|
2022-12-29 13:26:09 +08:00
|
|
|
|
randomIndex := rand.Intn(len(nums))
|
2022-12-27 11:25:30 +08:00
|
|
|
|
// 获取并返回随机元素
|
2022-12-28 10:46:12 +08:00
|
|
|
|
randomNum = nums[randomIndex]
|
2022-12-27 11:25:30 +08:00
|
|
|
|
return
|
2022-12-29 13:26:09 +08:00
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```javascript title="array.js"
|
|
|
|
|
/* 随机返回一个数组元素 */
|
2022-12-12 21:25:07 +08:00
|
|
|
|
function randomAccess(nums) {
|
2022-12-03 01:31:29 +08:00
|
|
|
|
// 在区间 [0, nums.length) 中随机抽取一个数字
|
2022-12-12 21:39:48 +08:00
|
|
|
|
const random_index = Math.floor(Math.random() * nums.length);
|
2022-12-03 01:31:29 +08:00
|
|
|
|
// 获取并返回随机元素
|
2022-12-12 21:25:07 +08:00
|
|
|
|
const random_num = nums[random_index];
|
2022-12-12 21:39:48 +08:00
|
|
|
|
return random_num;
|
2022-12-03 01:31:29 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="array.ts"
|
2022-12-04 18:00:44 +08:00
|
|
|
|
/* 随机返回一个数组元素 */
|
|
|
|
|
function randomAccess(nums: number[]): number {
|
|
|
|
|
// 在区间 [0, nums.length) 中随机抽取一个数字
|
2022-12-12 21:25:07 +08:00
|
|
|
|
const random_index = Math.floor(Math.random() * nums.length);
|
2022-12-04 18:00:44 +08:00
|
|
|
|
// 获取并返回随机元素
|
2022-12-12 21:25:07 +08:00
|
|
|
|
const random_num = nums[random_index];
|
|
|
|
|
return random_num;
|
2022-12-04 18:00:44 +08:00
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="array.c"
|
2022-12-09 21:47:24 +08:00
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="array.cs"
|
2022-12-14 15:11:25 +08:00
|
|
|
|
/* 随机返回一个数组元素 */
|
2022-12-14 11:03:28 +08:00
|
|
|
|
int RandomAccess(int[] nums)
|
|
|
|
|
{
|
|
|
|
|
Random random=new();
|
|
|
|
|
int randomIndex = random.Next(nums.Length);
|
|
|
|
|
int randomNum = nums[randomIndex];
|
|
|
|
|
return randomNum;
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-01-05 21:16:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="array.swift"
|
2023-01-08 20:53:24 +08:00
|
|
|
|
/* 随机返回一个数组元素 */
|
2023-01-05 21:16:05 +08:00
|
|
|
|
func randomAccess(nums: [Int]) -> Int {
|
|
|
|
|
// 在区间 [0, nums.count) 中随机抽取一个数字
|
|
|
|
|
let randomIndex = nums.indices.randomElement()!
|
|
|
|
|
// 获取并返回随机元素
|
|
|
|
|
let randomNum = nums[randomIndex]
|
|
|
|
|
return randomNum
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
## 数组缺点
|
|
|
|
|
|
2023-01-07 20:34:32 +08:00
|
|
|
|
**数组在初始化后长度不可变**。由于系统无法保证数组之后的内存空间是可用的,因此数组长度无法扩展。而若希望扩容数组,则需新建一个数组,然后把原数组元素依次拷贝到新数组,在数组很大的情况下,这是非常耗时的。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="array.java"
|
|
|
|
|
/* 扩展数组长度 */
|
|
|
|
|
int[] extend(int[] nums, int enlarge) {
|
|
|
|
|
// 初始化一个扩展长度后的数组
|
|
|
|
|
int[] res = new int[nums.length + enlarge];
|
|
|
|
|
// 将原数组中的所有元素复制到新数组
|
|
|
|
|
for (int i = 0; i < nums.length; i++) {
|
|
|
|
|
res[i] = nums[i];
|
|
|
|
|
}
|
|
|
|
|
// 返回扩展后的新数组
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="array.cpp"
|
2022-11-27 19:07:35 +08:00
|
|
|
|
/* 扩展数组长度 */
|
|
|
|
|
int* extend(int* nums, int size, int enlarge) {
|
|
|
|
|
// 初始化一个扩展长度后的数组
|
|
|
|
|
int* res = new int[size + enlarge];
|
|
|
|
|
// 将原数组中的所有元素复制到新数组
|
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
|
res[i] = nums[i];
|
|
|
|
|
}
|
|
|
|
|
// 返回扩展后的新数组
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="array.py"
|
2022-11-25 03:59:38 +08:00
|
|
|
|
""" 扩展数组长度 """
|
2022-11-27 20:00:03 +08:00
|
|
|
|
# 请注意,Python 的 list 是动态数组,可以直接扩展
|
|
|
|
|
# 为了方便学习,本函数将 list 看作是长度不可变的数组
|
2022-11-25 03:59:38 +08:00
|
|
|
|
def extend(nums, enlarge):
|
|
|
|
|
# 初始化一个扩展长度后的数组
|
|
|
|
|
res = [0] * (len(nums) + enlarge)
|
|
|
|
|
# 将原数组中的所有元素复制到新数组
|
|
|
|
|
for i in range(len(nums)):
|
|
|
|
|
res[i] = nums[i]
|
|
|
|
|
# 返回扩展后的新数组
|
|
|
|
|
return res
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="array.go"
|
2022-12-29 13:26:09 +08:00
|
|
|
|
/* 扩展数组长度 */
|
|
|
|
|
func extend(nums []int, enlarge int) []int {
|
|
|
|
|
// 初始化一个扩展长度后的数组
|
|
|
|
|
res := make([]int, len(nums)+enlarge)
|
|
|
|
|
// 将原数组中的所有元素复制到新数组
|
|
|
|
|
for i, num := range nums {
|
|
|
|
|
res[i] = num
|
|
|
|
|
}
|
|
|
|
|
// 返回扩展后的新数组
|
|
|
|
|
return res
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```javascript title="array.js"
|
|
|
|
|
/* 扩展数组长度 */
|
2022-12-12 21:25:07 +08:00
|
|
|
|
function extend(nums, enlarge) {
|
2022-12-03 01:31:29 +08:00
|
|
|
|
// 初始化一个扩展长度后的数组
|
2022-12-12 21:25:07 +08:00
|
|
|
|
const res = new Array(nums.length + enlarge).fill(0);
|
2022-12-03 01:31:29 +08:00
|
|
|
|
// 将原数组中的所有元素复制到新数组
|
2022-12-12 21:25:07 +08:00
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
|
|
|
|
res[i] = nums[i];
|
2022-12-03 01:31:29 +08:00
|
|
|
|
}
|
|
|
|
|
// 返回扩展后的新数组
|
2022-12-12 21:39:48 +08:00
|
|
|
|
return res;
|
2022-12-03 01:31:29 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="array.ts"
|
2022-12-04 18:00:44 +08:00
|
|
|
|
/* 扩展数组长度 */
|
|
|
|
|
function extend(nums: number[], enlarge: number): number[] {
|
|
|
|
|
// 初始化一个扩展长度后的数组
|
2022-12-12 21:25:07 +08:00
|
|
|
|
const res = new Array(nums.length + enlarge).fill(0);
|
2022-12-04 18:00:44 +08:00
|
|
|
|
// 将原数组中的所有元素复制到新数组
|
2022-12-12 21:25:07 +08:00
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
|
|
|
|
res[i] = nums[i];
|
2022-12-04 18:00:44 +08:00
|
|
|
|
}
|
|
|
|
|
// 返回扩展后的新数组
|
2022-12-12 21:25:07 +08:00
|
|
|
|
return res;
|
2022-12-04 18:00:44 +08:00
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="array.c"
|
2022-12-09 21:47:24 +08:00
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="array.cs"
|
2022-12-14 15:11:25 +08:00
|
|
|
|
/* 扩展数组长度 */
|
2022-12-14 11:03:28 +08:00
|
|
|
|
int[] Extend(int[] nums, int enlarge)
|
|
|
|
|
{
|
|
|
|
|
// 初始化一个扩展长度后的数组
|
|
|
|
|
int[] res = new int[nums.Length + enlarge];
|
|
|
|
|
// 将原数组中的所有元素复制到新数组
|
|
|
|
|
for (int i = 0; i < nums.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
res[i] = nums[i];
|
|
|
|
|
}
|
|
|
|
|
// 返回扩展后的新数组
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-01-05 21:16:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="array.swift"
|
2023-01-08 20:53:24 +08:00
|
|
|
|
/* 扩展数组长度 */
|
2023-01-05 21:16:05 +08:00
|
|
|
|
func extend(nums: [Int], enlarge: Int) -> [Int] {
|
|
|
|
|
// 初始化一个扩展长度后的数组
|
|
|
|
|
var res = Array(repeating: 0, count: nums.count + enlarge)
|
|
|
|
|
// 将原数组中的所有元素复制到新数组
|
|
|
|
|
for i in nums.indices {
|
|
|
|
|
res[i] = nums[i]
|
|
|
|
|
}
|
|
|
|
|
// 返回扩展后的新数组
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-01-07 20:34:32 +08:00
|
|
|
|
**数组中插入或删除元素效率低下**。假设我们想要在数组中间某位置插入一个元素,由于数组元素在内存中是“紧挨着的”,它们之间没有空间再放任何数据。因此,我们不得不将此索引之后的所有元素都向后移动一位,然后再把元素赋值给该索引。删除元素也是类似,需要把此索引之后的元素都向前移动一位。总体看有以下缺点:
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-01-09 22:39:30 +08:00
|
|
|
|
- **时间复杂度高**:数组的插入和删除的平均时间复杂度均为 $O(N)$ ,其中 $N$ 为数组长度。
|
|
|
|
|
- **丢失元素**:由于数组的长度不可变,因此在插入元素后,超出数组长度范围的元素会被丢失。
|
|
|
|
|
- **内存浪费**:我们一般会初始化一个比较长的数组,只用前面一部分,这样在插入数据时,丢失的末尾元素都是我们不关心的,但这样做同时也会造成内存空间的浪费。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
![array_insert_remove_element](array.assets/array_insert_remove_element.png)
|
|
|
|
|
|
2022-11-23 15:50:59 +08:00
|
|
|
|
<p align="center"> Fig. 在数组中插入与删除元素 </p>
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="array.java"
|
|
|
|
|
/* 在数组的索引 index 处插入元素 num */
|
|
|
|
|
void insert(int[] nums, int num, int index) {
|
|
|
|
|
// 把索引 index 以及之后的所有元素向后移动一位
|
2022-12-20 00:21:52 +08:00
|
|
|
|
for (int i = nums.length - 1; i > index; i--) {
|
2022-11-22 17:47:26 +08:00
|
|
|
|
nums[i] = nums[i - 1];
|
|
|
|
|
}
|
|
|
|
|
// 将 num 赋给 index 处元素
|
|
|
|
|
nums[index] = num;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 删除索引 index 处元素 */
|
|
|
|
|
void remove(int[] nums, int index) {
|
|
|
|
|
// 把索引 index 之后的所有元素向前移动一位
|
|
|
|
|
for (int i = index; i < nums.length - 1; i++) {
|
|
|
|
|
nums[i] = nums[i + 1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="array.cpp"
|
2022-11-27 19:07:35 +08:00
|
|
|
|
/* 在数组的索引 index 处插入元素 num */
|
|
|
|
|
void insert(int* nums, int size, int num, int index) {
|
|
|
|
|
// 把索引 index 以及之后的所有元素向后移动一位
|
2022-12-20 00:21:52 +08:00
|
|
|
|
for (int i = size - 1; i > index; i--) {
|
2022-11-27 19:07:35 +08:00
|
|
|
|
nums[i] = nums[i - 1];
|
|
|
|
|
}
|
|
|
|
|
// 将 num 赋给 index 处元素
|
|
|
|
|
nums[index] = num;
|
|
|
|
|
}
|
2022-12-09 21:47:24 +08:00
|
|
|
|
|
2022-11-27 19:07:35 +08:00
|
|
|
|
/* 删除索引 index 处元素 */
|
|
|
|
|
void remove(int* nums, int size, int index) {
|
|
|
|
|
// 把索引 index 之后的所有元素向前移动一位
|
|
|
|
|
for (int i = index; i < size - 1; i++) {
|
|
|
|
|
nums[i] = nums[i + 1];
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="array.py"
|
2022-11-25 03:59:38 +08:00
|
|
|
|
""" 在数组的索引 index 处插入元素 num """
|
|
|
|
|
def insert(nums, num, index):
|
|
|
|
|
# 把索引 index 以及之后的所有元素向后移动一位
|
2022-12-20 13:20:20 +08:00
|
|
|
|
for i in range(len(nums) - 1, index, -1):
|
2022-11-25 03:59:38 +08:00
|
|
|
|
nums[i] = nums[i - 1]
|
|
|
|
|
# 将 num 赋给 index 处元素
|
|
|
|
|
nums[index] = num
|
2022-12-09 21:47:24 +08:00
|
|
|
|
|
2022-11-25 03:59:38 +08:00
|
|
|
|
""" 删除索引 index 处元素 """
|
|
|
|
|
def remove(nums, index):
|
|
|
|
|
# 把索引 index 之后的所有元素向前移动一位
|
|
|
|
|
for i in range(index, len(nums) - 1):
|
|
|
|
|
nums[i] = nums[i + 1]
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="array.go"
|
2022-12-29 13:26:09 +08:00
|
|
|
|
/* 在数组的索引 index 处插入元素 num */
|
2022-12-28 10:46:12 +08:00
|
|
|
|
func insert(nums []int, num int, index int) {
|
2022-12-29 13:26:09 +08:00
|
|
|
|
// 把索引 index 以及之后的所有元素向后移动一位
|
2022-12-27 11:25:30 +08:00
|
|
|
|
for i := len(nums) - 1; i > index; i-- {
|
2022-12-29 13:26:09 +08:00
|
|
|
|
nums[i] = nums[i-1]
|
|
|
|
|
}
|
|
|
|
|
// 将 num 赋给 index 处元素
|
|
|
|
|
nums[index] = num
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 删除索引 index 处元素 */
|
2022-12-28 10:46:12 +08:00
|
|
|
|
func remove(nums []int, index int) {
|
2022-12-29 13:26:09 +08:00
|
|
|
|
// 把索引 index 之后的所有元素向前移动一位
|
2023-01-02 19:03:36 +08:00
|
|
|
|
for i := index; i < len(nums)-1; i++ {
|
2022-12-29 13:26:09 +08:00
|
|
|
|
nums[i] = nums[i+1]
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```javascript title="array.js"
|
|
|
|
|
/* 在数组的索引 index 处插入元素 num */
|
2022-12-12 21:25:07 +08:00
|
|
|
|
function insert(nums, num, index) {
|
2022-12-03 01:31:29 +08:00
|
|
|
|
// 把索引 index 以及之后的所有元素向后移动一位
|
2022-12-20 00:21:52 +08:00
|
|
|
|
for (let i = nums.length - 1; i > index; i--) {
|
2022-12-03 01:31:29 +08:00
|
|
|
|
nums[i] = nums[i - 1];
|
|
|
|
|
}
|
|
|
|
|
// 将 num 赋给 index 处元素
|
|
|
|
|
nums[index] = num;
|
|
|
|
|
}
|
2022-12-14 15:11:25 +08:00
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
/* 删除索引 index 处元素 */
|
2022-12-12 21:25:07 +08:00
|
|
|
|
function remove(nums, index) {
|
2022-12-03 01:31:29 +08:00
|
|
|
|
// 把索引 index 之后的所有元素向前移动一位
|
|
|
|
|
for (let i = index; i < nums.length - 1; i++) {
|
2022-12-12 21:39:48 +08:00
|
|
|
|
nums[i] = nums[i + 1];
|
2022-12-03 01:31:29 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="array.ts"
|
2022-12-04 18:00:44 +08:00
|
|
|
|
/* 在数组的索引 index 处插入元素 num */
|
|
|
|
|
function insert(nums: number[], num: number, index: number): void {
|
|
|
|
|
// 把索引 index 以及之后的所有元素向后移动一位
|
2022-12-20 00:21:52 +08:00
|
|
|
|
for (let i = nums.length - 1; i > index; i--) {
|
2022-12-12 21:25:07 +08:00
|
|
|
|
nums[i] = nums[i - 1];
|
2022-12-04 18:00:44 +08:00
|
|
|
|
}
|
|
|
|
|
// 将 num 赋给 index 处元素
|
2022-12-12 21:25:07 +08:00
|
|
|
|
nums[index] = num;
|
2022-12-04 18:00:44 +08:00
|
|
|
|
}
|
2022-12-09 21:47:24 +08:00
|
|
|
|
|
2022-12-04 18:00:44 +08:00
|
|
|
|
/* 删除索引 index 处元素 */
|
|
|
|
|
function remove(nums: number[], index: number): void {
|
|
|
|
|
// 把索引 index 之后的所有元素向前移动一位
|
|
|
|
|
for (let i = index; i < nums.length - 1; i++) {
|
2022-12-12 21:25:07 +08:00
|
|
|
|
nums[i] = nums[i + 1];
|
2022-12-04 18:00:44 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="array.c"
|
2022-12-09 21:47:24 +08:00
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="array.cs"
|
2022-12-14 15:11:25 +08:00
|
|
|
|
/* 在数组的索引 index 处插入元素 num */
|
2022-12-14 11:03:28 +08:00
|
|
|
|
void Insert(int[] nums, int num, int index)
|
|
|
|
|
{
|
|
|
|
|
// 把索引 index 以及之后的所有元素向后移动一位
|
2022-12-20 00:21:52 +08:00
|
|
|
|
for (int i = nums.Length - 1; i > index; i--)
|
2022-12-14 11:03:28 +08:00
|
|
|
|
{
|
|
|
|
|
nums[i] = nums[i - 1];
|
|
|
|
|
}
|
|
|
|
|
// 将 num 赋给 index 处元素
|
|
|
|
|
nums[index] = num;
|
|
|
|
|
}
|
2022-12-14 15:11:25 +08:00
|
|
|
|
/* 删除索引 index 处元素 */
|
2022-12-14 11:03:28 +08:00
|
|
|
|
void Remove(int[] nums, int index)
|
|
|
|
|
{
|
|
|
|
|
// 把索引 index 之后的所有元素向前移动一位
|
|
|
|
|
for (int i = index; i < nums.Length - 1; i++)
|
|
|
|
|
{
|
|
|
|
|
nums[i] = nums[i + 1];
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-01-05 21:16:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="array.swift"
|
2023-01-08 20:53:24 +08:00
|
|
|
|
/* 在数组的索引 index 处插入元素 num */
|
2023-01-05 21:16:05 +08:00
|
|
|
|
func insert(nums: inout [Int], num: Int, index: Int) {
|
|
|
|
|
// 把索引 index 以及之后的所有元素向后移动一位
|
|
|
|
|
for i in sequence(first: nums.count - 1, next: { $0 > index + 1 ? $0 - 1 : nil }) {
|
|
|
|
|
nums[i] = nums[i - 1]
|
|
|
|
|
}
|
|
|
|
|
// 将 num 赋给 index 处元素
|
|
|
|
|
nums[index] = num
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-08 20:53:24 +08:00
|
|
|
|
/* 删除索引 index 处元素 */
|
2023-01-05 21:16:05 +08:00
|
|
|
|
func remove(nums: inout [Int], index: Int) {
|
|
|
|
|
let count = nums.count
|
|
|
|
|
// 把索引 index 之后的所有元素向前移动一位
|
|
|
|
|
for i in sequence(first: index, next: { $0 < count - 1 - 1 ? $0 + 1 : nil }) {
|
|
|
|
|
nums[i] = nums[i + 1]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
## 数组常用操作
|
|
|
|
|
|
2023-01-07 20:34:32 +08:00
|
|
|
|
**数组遍历**。以下介绍两种常用的遍历方法。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="array.java"
|
|
|
|
|
/* 遍历数组 */
|
|
|
|
|
void traverse(int[] nums) {
|
|
|
|
|
int count = 0;
|
|
|
|
|
// 通过索引遍历数组
|
|
|
|
|
for (int i = 0; i < nums.length; i++) {
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
// 直接遍历数组
|
|
|
|
|
for (int num : nums) {
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="array.cpp"
|
2022-11-27 19:07:35 +08:00
|
|
|
|
/* 遍历数组 */
|
|
|
|
|
void traverse(int* nums, int size) {
|
|
|
|
|
int count = 0;
|
|
|
|
|
// 通过索引遍历数组
|
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="array.py"
|
2022-11-25 03:59:38 +08:00
|
|
|
|
""" 遍历数组 """
|
|
|
|
|
def traverse(nums):
|
|
|
|
|
count = 0
|
|
|
|
|
# 通过索引遍历数组
|
|
|
|
|
for i in range(len(nums)):
|
|
|
|
|
count += 1
|
|
|
|
|
# 直接遍历数组
|
|
|
|
|
for num in nums:
|
|
|
|
|
count += 1
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="array.go"
|
2022-12-29 13:26:09 +08:00
|
|
|
|
/* 遍历数组 */
|
|
|
|
|
func traverse(nums []int) {
|
|
|
|
|
count := 0
|
|
|
|
|
// 通过索引遍历数组
|
|
|
|
|
for i := 0; i < len(nums); i++ {
|
|
|
|
|
count++
|
|
|
|
|
}
|
|
|
|
|
// 直接遍历数组
|
|
|
|
|
for range nums {
|
|
|
|
|
count++
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```javascript title="array.js"
|
|
|
|
|
/* 遍历数组 */
|
2022-12-12 21:25:07 +08:00
|
|
|
|
function traverse(nums) {
|
2022-12-12 21:39:48 +08:00
|
|
|
|
let count = 0;
|
2022-12-03 01:31:29 +08:00
|
|
|
|
// 通过索引遍历数组
|
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
// 直接遍历数组
|
2022-12-12 21:25:07 +08:00
|
|
|
|
for (let num of nums) {
|
2022-12-12 21:39:48 +08:00
|
|
|
|
count += 1;
|
2022-12-03 01:31:29 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="array.ts"
|
2022-12-04 18:00:44 +08:00
|
|
|
|
/* 遍历数组 */
|
|
|
|
|
function traverse(nums: number[]): void {
|
2022-12-12 21:25:07 +08:00
|
|
|
|
let count = 0;
|
2022-12-04 18:00:44 +08:00
|
|
|
|
// 通过索引遍历数组
|
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
2022-12-12 21:25:07 +08:00
|
|
|
|
count++;
|
2022-12-04 18:00:44 +08:00
|
|
|
|
}
|
|
|
|
|
// 直接遍历数组
|
|
|
|
|
for(let num of nums){
|
2022-12-12 21:25:07 +08:00
|
|
|
|
count += 1;
|
2022-12-04 18:00:44 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="array.c"
|
2022-12-09 21:47:24 +08:00
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="array.cs"
|
2022-12-14 15:11:25 +08:00
|
|
|
|
/* 遍历数组 */
|
2022-12-14 11:03:28 +08:00
|
|
|
|
void Traverse(int[] nums)
|
|
|
|
|
{
|
|
|
|
|
int count = 0;
|
|
|
|
|
// 通过索引遍历数组
|
|
|
|
|
for (int i = 0; i < nums.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
// 直接遍历数组
|
|
|
|
|
foreach (int num in nums)
|
|
|
|
|
{
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-01-05 21:16:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="array.swift"
|
2023-01-08 20:53:24 +08:00
|
|
|
|
/* 遍历数组 */
|
2023-01-05 21:16:05 +08:00
|
|
|
|
func traverse(nums: [Int]) {
|
|
|
|
|
var count = 0
|
|
|
|
|
// 通过索引遍历数组
|
|
|
|
|
for _ in nums.indices {
|
|
|
|
|
count += 1
|
|
|
|
|
}
|
|
|
|
|
// 直接遍历数组
|
|
|
|
|
for _ in nums {
|
|
|
|
|
count += 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-01-07 20:34:32 +08:00
|
|
|
|
**数组查找**。通过遍历数组,查找数组内的指定元素,并输出对应索引。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="array.java"
|
|
|
|
|
/* 在数组中查找指定元素 */
|
|
|
|
|
int find(int[] nums, int target) {
|
|
|
|
|
for (int i = 0; i < nums.length; i++) {
|
|
|
|
|
if (nums[i] == target)
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="array.cpp"
|
2022-11-27 19:07:35 +08:00
|
|
|
|
/* 在数组中查找指定元素 */
|
|
|
|
|
int find(int* nums, int size, int target) {
|
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
|
if (nums[i] == target)
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="array.py"
|
2022-11-25 03:59:38 +08:00
|
|
|
|
""" 在数组中查找指定元素 """
|
|
|
|
|
def find(nums, target):
|
|
|
|
|
for i in range(len(nums)):
|
|
|
|
|
if nums[i] == target:
|
|
|
|
|
return i
|
|
|
|
|
return -1
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="array.go"
|
2022-12-27 11:25:30 +08:00
|
|
|
|
/* 在数组中查找指定元素 */
|
2022-12-28 10:46:12 +08:00
|
|
|
|
func find(nums []int, target int) (index int) {
|
|
|
|
|
index = -1
|
2022-12-29 13:26:09 +08:00
|
|
|
|
for i := 0; i < len(nums); i++ {
|
|
|
|
|
if nums[i] == target {
|
2022-12-28 10:46:12 +08:00
|
|
|
|
index = i
|
2022-12-27 11:25:30 +08:00
|
|
|
|
break
|
2022-12-29 13:26:09 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-27 11:25:30 +08:00
|
|
|
|
return
|
2022-12-29 13:26:09 +08:00
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
```javascript title="array.js"
|
|
|
|
|
/* 在数组中查找指定元素 */
|
2022-12-12 21:25:07 +08:00
|
|
|
|
function find(nums, target) {
|
2022-12-03 01:31:29 +08:00
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
2022-12-12 21:25:07 +08:00
|
|
|
|
if (nums[i] == target) return i;
|
2022-12-03 01:31:29 +08:00
|
|
|
|
}
|
2022-12-12 21:39:48 +08:00
|
|
|
|
return -1;
|
2022-12-03 01:31:29 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
```typescript title="array.ts"
|
2022-12-04 18:00:44 +08:00
|
|
|
|
/* 在数组中查找指定元素 */
|
|
|
|
|
function find(nums: number[], target: number): number {
|
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
|
|
|
|
if (nums[i] === target) {
|
2022-12-12 21:25:07 +08:00
|
|
|
|
return i;
|
2022-12-04 18:00:44 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-12 21:25:07 +08:00
|
|
|
|
return -1;
|
2022-12-04 18:00:44 +08:00
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="array.c"
|
2022-12-09 21:47:24 +08:00
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="array.cs"
|
2022-12-14 11:22:14 +08:00
|
|
|
|
/* 在数组中查找指定元素 */
|
2022-12-14 11:03:28 +08:00
|
|
|
|
int Find(int[] nums, int target)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < nums.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (nums[i] == target)
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-01-05 21:16:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="array.swift"
|
2023-01-08 20:53:24 +08:00
|
|
|
|
/* 在数组中查找指定元素 */
|
2023-01-05 21:16:05 +08:00
|
|
|
|
func find(nums: [Int], target: Int) -> Int {
|
|
|
|
|
for i in nums.indices {
|
|
|
|
|
if nums[i] == target {
|
|
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
## 数组典型应用
|
|
|
|
|
|
2023-01-07 20:34:32 +08:00
|
|
|
|
**随机访问**。如果我们想要随机抽取一些样本,那么可以用数组存储,并生成一个随机序列,根据索引实现样本的随机抽取。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-01-07 20:34:32 +08:00
|
|
|
|
**二分查找**。例如前文查字典的例子,我们可以将字典中的所有字按照拼音顺序存储在数组中,然后使用与日常查纸质字典相同的“翻开中间,排除一半”的方式,来实现一个查电子字典的算法。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-01-07 20:34:32 +08:00
|
|
|
|
**深度学习**。神经网络中大量使用了向量、矩阵、张量之间的线性代数运算,这些数据都是以数组的形式构建的。数组是神经网络编程中最常使用的数据结构。
|