Organizing all the code blocks.

This commit is contained in:
Yudong Jin
2022-12-03 01:31:29 +08:00
parent fec56afd5f
commit 9bd5980a81
21 changed files with 2520 additions and 310 deletions

View File

@@ -35,6 +35,36 @@ comments: true
list = [1, 3, 2, 5, 4]
```
=== "Go"
```go title="list.go"
```
=== "JavaScript"
```js title="list.js"
```
=== "TypeScript"
```typescript title="list.ts"
```
=== "C"
```c title="list.c"
```
=== "C#"
```csharp title="list.cs"
```
**访问与更新元素。** 列表的底层数据结构是数组,因此可以在 $O(1)$ 时间内访问与更新元素,效率很高。
=== "Java"
@@ -67,6 +97,36 @@ comments: true
list[1] = 0 # 将索引 1 处的元素更新为 0
```
=== "Go"
```go title="list.go"
```
=== "JavaScript"
```js title="list.js"
```
=== "TypeScript"
```typescript title="list.ts"
```
=== "C"
```c title="list.c"
```
=== "C#"
```csharp title="list.cs"
```
**在列表中添加、插入、删除元素。** 相对于数组,列表可以自由地添加与删除元素。在列表尾部添加元素的时间复杂度为 $O(1)$ ,但是插入与删除元素的效率仍与数组一样低,时间复杂度为 $O(N)$ 。
=== "Java"
@@ -129,6 +189,36 @@ comments: true
list.pop(3) # 删除索引 3 处的元素
```
=== "Go"
```go title="list.go"
```
=== "JavaScript"
```js title="list.js"
```
=== "TypeScript"
```typescript title="list.ts"
```
=== "C"
```c title="list.c"
```
=== "C#"
```csharp title="list.cs"
```
**遍历列表。** 与数组一样,列表可以使用索引遍历,也可以使用 `for-each` 直接遍历。
=== "Java"
@@ -177,6 +267,36 @@ comments: true
count += 1
```
=== "Go"
```go title="list.go"
```
=== "JavaScript"
```js title="list.js"
```
=== "TypeScript"
```typescript title="list.ts"
```
=== "C"
```c title="list.c"
```
=== "C#"
```csharp title="list.cs"
```
**拼接两个列表。** 再创建一个新列表 `list1` ,我们可以将其中一个列表拼接到另一个的尾部。
=== "Java"
@@ -204,6 +324,36 @@ comments: true
list += list1 # 将列表 list1 拼接到 list 之后
```
=== "Go"
```go title="list.go"
```
=== "JavaScript"
```js title="list.js"
```
=== "TypeScript"
```typescript title="list.ts"
```
=== "C"
```c title="list.c"
```
=== "C#"
```csharp title="list.cs"
```
**排序列表。** 排序也是常用的方法之一,完成列表排序后,我们就可以使用在数组类算法题中经常考察的「二分查找」和「双指针」算法了。
=== "Java"
@@ -227,6 +377,36 @@ comments: true
list.sort() # 排序后,列表元素从小到大排列
```
=== "Go"
```go title="list.go"
```
=== "JavaScript"
```js title="list.js"
```
=== "TypeScript"
```typescript title="list.ts"
```
=== "C"
```c title="list.c"
```
=== "C#"
```csharp title="list.cs"
```
## 列表简易实现 *
为了帮助加深对列表的理解,我们在此提供一个列表的简易版本的实现。需要关注三个核心点:
@@ -491,3 +671,33 @@ comments: true
# 更新列表容量
self.__capacity = len(self.__nums)
```
=== "Go"
```go title="my_list.go"
```
=== "JavaScript"
```js title="my_list.js"
```
=== "TypeScript"
```typescript title="my_list.ts"
```
=== "C"
```c title="my_list.c"
```
=== "C#"
```csharp title="my_list.cs"
```