Add Python codes for the chapter

of array and linked list.
This commit is contained in:
krahets
2022-11-25 03:59:38 +08:00
parent 9a861140d8
commit cbf4ab0aaa
8 changed files with 503 additions and 22 deletions

View File

@@ -30,7 +30,8 @@ comments: true
=== "Python"
```python title="list.py"
""" 初始化列表 """
list = [1, 3, 2, 5, 4]
```
**访问与更新元素。** 列表的底层数据结构是数组,因此可以在 $O(1)$ 时间内访问与更新元素,效率很高。
@@ -54,7 +55,11 @@ comments: true
=== "Python"
```python title="list.py"
""" 访问元素 """
num = list[1] # 访问索引 1 处的元素
""" 更新元素 """
list[1] = 0 # 将索引 1 处的元素更新为 0
```
**在列表中添加、插入、删除元素。** 相对于数组,列表可以自由地添加与删除元素。在列表尾部添加元素的时间复杂度为 $O(1)$ ,但是插入与删除元素的效率仍与数组一样低,时间复杂度为 $O(N)$ 。
@@ -88,7 +93,21 @@ comments: true
=== "Python"
```python title="list.py"
""" 清空列表 """
list.clear()
""" 尾部添加元素 """
list.append(1)
list.append(3)
list.append(2)
list.append(5)
list.append(4)
""" 中间插入元素 """
list.insert(3, 6) # 在索引 3 处插入数字 6
""" 删除元素 """
list.pop(3) # 删除索引 3 处的元素
```
**遍历列表。** 与数组一样,列表可以使用索引遍历,也可以使用 `for-each` 直接遍历。
@@ -118,7 +137,15 @@ comments: true
=== "Python"
```python title="list.py"
""" 通过索引遍历列表 """
count = 0
for i in range(len(list)):
count += 1
""" 直接遍历列表元素 """
count = 0
for n in list:
count += 1
```
**拼接两个列表。** 再创建一个新列表 `list1` ,我们可以将其中一个列表拼接到另一个的尾部。
@@ -140,7 +167,9 @@ comments: true
=== "Python"
```python title="list.py"
""" 拼接两个列表 """
list1 = [6, 8, 7, 10, 9]
list += list1 # 将列表 list1 拼接到 list 之后
```
**排序列表。** 排序也是常用的方法之一,完成列表排序后,我们就可以使用在数组类算法题中经常考察的「二分查找」和「双指针」算法了。
@@ -161,7 +190,8 @@ comments: true
=== "Python"
```python title="list.py"
""" 排序列表 """
list.sort() # 排序后,列表元素从小到大排列
```
## 列表简易实现 *
@@ -189,12 +219,12 @@ comments: true
nums = new int[initialCapacity];
}
/* 获取列表容量 */
/* 获取列表长度(即当前元素数量)*/
public int size() {
return size;
}
/* 获取列表长度(即当前元素数量) */
/* 获取列表容量 */
public int capacity() {
return nums.length;
}
@@ -269,5 +299,60 @@ comments: true
=== "Python"
```python title="my_list.py"
""" 列表类简易实现 """
class MyList:
""" 构造函数 """
def __init__(self):
self._initial_capacity = 10 # 列表初始容量
self._nums = [0] * self._initial_capacity # 数组(存储列表元素)
self._size = 0 # 列表长度(即当前元素数量)
self._extend_ratio = 2 # 每次列表扩容的倍数
""" 获取列表长度(即当前元素数量) """
def size(self):
return self._size
""" 获取列表容量 """
def capacity(self):
return len(self._nums)
""" 访问元素 """
def get(self, index):
# 索引如果越界则抛出异常,下同
assert index < self._size, "索引越界"
return self._nums[index]
""" 更新元素 """
def set(self, num, index):
assert index < self._size, "索引越界"
self._nums[index] = num
""" 中间插入元素 """
def add(self, num, index=-1):
assert index < self._size, "索引越界"
if index == -1:
index = self._size
# 元素数量超出容量时,触发扩容机制
if self._size == self.capacity():
self.extend_capacity()
# 索引 i 以及之后的元素都向后移动一位
for j in range(self._size - 1, index - 1, -1):
self._nums[j + 1] = self._nums[j]
self._nums[index] = num
# 更新元素数量
self._size += 1
""" 删除元素 """
def remove(self, index):
assert index < self._size, "索引越界"
# 索引 i 之后的元素都向前移动一位
for j in range(index, self._size - 1):
self._nums[j] = self._nums[j + 1]
# 更新元素数量
self._size -= 1
""" 列表扩容 """
def extend_capacity(self):
# 新建一个长度为 self._size 的数组,并将原数组拷贝到新数组
self._nums = self._nums + [0] * self.capacity() * (self._extend_ratio - 1)
```