mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 04:31:55 +08:00
feat: Revised the book (#978)
* Sync recent changes to the revised Word. * Revised the preface chapter * Revised the introduction chapter * Revised the computation complexity chapter * Revised the chapter data structure * Revised the chapter array and linked list * Revised the chapter stack and queue * Revised the chapter hashing * Revised the chapter tree * Revised the chapter heap * Revised the chapter graph * Revised the chapter searching * Reivised the sorting chapter * Revised the divide and conquer chapter * Revised the chapter backtacking * Revised the DP chapter * Revised the greedy chapter * Revised the appendix chapter * Revised the preface chapter doubly * Revised the figures
This commit is contained in:
@ -17,7 +17,7 @@ def random_access(nums: list[int]) -> int:
|
||||
|
||||
|
||||
# 请注意,Python 的 list 是动态数组,可以直接扩展
|
||||
# 为了方便学习,本函数将 list 看作是长度不可变的数组
|
||||
# 为了方便学习,本函数将 list 看作长度不可变的数组
|
||||
def extend(nums: list[int], enlarge: int) -> list[int]:
|
||||
"""扩展数组长度"""
|
||||
# 初始化一个扩展长度后的数组
|
||||
@ -34,12 +34,12 @@ def insert(nums: list[int], num: int, index: int):
|
||||
# 把索引 index 以及之后的所有元素向后移动一位
|
||||
for i in range(len(nums) - 1, index, -1):
|
||||
nums[i] = nums[i - 1]
|
||||
# 将 num 赋给 index 处元素
|
||||
# 将 num 赋给 index 处的元素
|
||||
nums[index] = num
|
||||
|
||||
|
||||
def remove(nums: list[int], index: int):
|
||||
"""删除索引 index 处元素"""
|
||||
"""删除索引 index 处的元素"""
|
||||
# 把索引 index 之后的所有元素向前移动一位
|
||||
for i in range(index, len(nums) - 1):
|
||||
nums[i] = nums[i + 1]
|
||||
|
||||
@ -57,7 +57,7 @@ if __name__ == "__main__":
|
||||
n2 = ListNode(2)
|
||||
n3 = ListNode(5)
|
||||
n4 = ListNode(4)
|
||||
# 构建引用指向
|
||||
# 构建节点之间的引用
|
||||
n0.next = n1
|
||||
n1.next = n2
|
||||
n2.next = n3
|
||||
|
||||
@ -22,7 +22,7 @@ if __name__ == "__main__":
|
||||
nums.clear()
|
||||
print("\n清空列表后 nums =", nums)
|
||||
|
||||
# 尾部添加元素
|
||||
# 在尾部添加元素
|
||||
nums.append(1)
|
||||
nums.append(3)
|
||||
nums.append(2)
|
||||
@ -30,7 +30,7 @@ if __name__ == "__main__":
|
||||
nums.append(4)
|
||||
print("\n添加元素后 nums =", nums)
|
||||
|
||||
# 中间插入元素
|
||||
# 在中间插入元素
|
||||
nums.insert(3, 6)
|
||||
print("\n在索引 3 处插入数字 6 ,得到 nums =", nums)
|
||||
|
||||
|
||||
@ -6,17 +6,17 @@ Author: Krahets (krahets@163.com)
|
||||
|
||||
|
||||
class MyList:
|
||||
"""列表类简易实现"""
|
||||
"""列表类"""
|
||||
|
||||
def __init__(self):
|
||||
"""构造方法"""
|
||||
self._capacity: int = 10 # 列表容量
|
||||
self._arr: list[int] = [0] * self._capacity # 数组(存储列表元素)
|
||||
self._size: int = 0 # 列表长度(即当前元素数量)
|
||||
self._size: int = 0 # 列表长度(当前元素数量)
|
||||
self._extend_ratio: int = 2 # 每次列表扩容的倍数
|
||||
|
||||
def size(self) -> int:
|
||||
"""获取列表长度(即当前元素数量)"""
|
||||
"""获取列表长度(当前元素数量)"""
|
||||
return self._size
|
||||
|
||||
def capacity(self) -> int:
|
||||
@ -37,7 +37,7 @@ class MyList:
|
||||
self._arr[index] = num
|
||||
|
||||
def add(self, num: int):
|
||||
"""尾部添加元素"""
|
||||
"""在尾部添加元素"""
|
||||
# 元素数量超出容量时,触发扩容机制
|
||||
if self.size() == self.capacity():
|
||||
self.extend_capacity()
|
||||
@ -45,7 +45,7 @@ class MyList:
|
||||
self._size += 1
|
||||
|
||||
def insert(self, num: int, index: int):
|
||||
"""中间插入元素"""
|
||||
"""在中间插入元素"""
|
||||
if index < 0 or index >= self._size:
|
||||
raise IndexError("索引越界")
|
||||
# 元素数量超出容量时,触发扩容机制
|
||||
@ -87,7 +87,7 @@ class MyList:
|
||||
if __name__ == "__main__":
|
||||
# 初始化列表
|
||||
nums = MyList()
|
||||
# 尾部添加元素
|
||||
# 在尾部添加元素
|
||||
nums.add(1)
|
||||
nums.add(3)
|
||||
nums.add(2)
|
||||
@ -95,7 +95,7 @@ if __name__ == "__main__":
|
||||
nums.add(4)
|
||||
print(f"列表 nums = {nums.to_array()} ,容量 = {nums.capacity()} ,长度 = {nums.size()}")
|
||||
|
||||
# 中间插入元素
|
||||
# 在中间插入元素
|
||||
nums.insert(6, index=3)
|
||||
print("在索引 3 处插入数字 6 ,得到 nums =", nums.to_array())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user