mirror of
https://github.com/krahets/hello-algo.git
synced 2025-01-23 06:00:27 +08:00
Unify the comment style of python codes
This commit is contained in:
parent
5ddcb60825
commit
10e2180013
@ -64,7 +64,7 @@ def find(nums: list[int], target: int) -> int:
|
||||
return -1
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化数组
|
||||
arr: list[int] = [0] * 5
|
||||
|
@ -47,7 +47,7 @@ def find(head: ListNode, target: int) -> int:
|
||||
return -1
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化链表
|
||||
# 初始化各个节点
|
||||
|
@ -4,7 +4,7 @@ Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化列表
|
||||
arr: list[int] = [1, 3, 2, 5, 4]
|
||||
|
@ -79,7 +79,7 @@ class MyList:
|
||||
return self.__nums[: self.__size]
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化列表
|
||||
my_list = MyList()
|
||||
|
@ -27,7 +27,7 @@ def two_sum_hash_table(nums: list[int], target: int) -> list[int]:
|
||||
return []
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# ======= Test Case =======
|
||||
nums = [2, 7, 11, 15]
|
||||
|
@ -73,7 +73,7 @@ def build_tree(n: int) -> TreeNode | None:
|
||||
return root
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
n = 5
|
||||
# 常数阶
|
||||
|
@ -114,7 +114,7 @@ def factorial_recur(n: int) -> int:
|
||||
return count
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势
|
||||
n = 8
|
||||
|
@ -26,7 +26,7 @@ def find_one(nums: list[int]) -> int:
|
||||
return -1
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
for i in range(10):
|
||||
n: int = 100
|
||||
|
@ -69,7 +69,7 @@ class GraphAdjList:
|
||||
print(f"{vertex.val}: {tmp},")
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化无向图
|
||||
v = vals_to_vets([1, 3, 2, 5, 4])
|
||||
|
@ -84,7 +84,7 @@ class GraphAdjMat:
|
||||
print_matrix(self.adj_mat)
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化无向图
|
||||
# 请注意,edges 元素代表顶点索引,即对应 vertices 元素索引
|
||||
|
@ -35,7 +35,7 @@ def graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:
|
||||
return res
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化无向图
|
||||
v = vals_to_vets([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||
|
@ -34,7 +34,7 @@ def graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:
|
||||
return res
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化无向图
|
||||
v = vals_to_vets([0, 1, 2, 3, 4, 5, 6])
|
||||
|
@ -77,7 +77,7 @@ class ArrayHashMap:
|
||||
print(pair.key, "->", pair.val)
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化哈希表
|
||||
mapp = ArrayHashMap()
|
||||
|
@ -9,7 +9,7 @@ import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from modules import *
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化哈希表
|
||||
mapp = dict[int, str]()
|
||||
|
@ -24,7 +24,7 @@ def test_pop(heap: list, flag: int = 1) -> None:
|
||||
print_heap([flag * val for val in heap])
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化小顶堆
|
||||
min_heap, flag = [], 1
|
||||
@ -35,33 +35,34 @@ if __name__ == "__main__":
|
||||
# Python 的 heapq 模块默认实现小顶堆
|
||||
# 考虑将“元素取负”后再入堆,这样就可以将大小关系颠倒,从而实现大顶堆
|
||||
# 在本示例中,flag = 1 时对应小顶堆,flag = -1 时对应大顶堆
|
||||
""" 元素入堆 """
|
||||
|
||||
# 元素入堆
|
||||
test_push(max_heap, 1, flag)
|
||||
test_push(max_heap, 3, flag)
|
||||
test_push(max_heap, 2, flag)
|
||||
test_push(max_heap, 5, flag)
|
||||
test_push(max_heap, 4, flag)
|
||||
|
||||
""" 获取堆顶元素 """
|
||||
# 获取堆顶元素
|
||||
peek: int = flag * max_heap[0]
|
||||
print(f"\n堆顶元素为 {peek}")
|
||||
|
||||
""" 堆顶元素出堆 """
|
||||
# 堆顶元素出堆
|
||||
test_pop(max_heap, flag)
|
||||
test_pop(max_heap, flag)
|
||||
test_pop(max_heap, flag)
|
||||
test_pop(max_heap, flag)
|
||||
test_pop(max_heap, flag)
|
||||
|
||||
""" 获取堆大小 """
|
||||
# 获取堆大小
|
||||
size: int = len(max_heap)
|
||||
print(f"\n堆元素数量为 {size}")
|
||||
|
||||
""" 判断堆是否为空 """
|
||||
# 判断堆是否为空
|
||||
is_empty: bool = not max_heap
|
||||
print(f"\n堆是否为空 {is_empty}")
|
||||
|
||||
""" 输入列表并建堆 """
|
||||
# 输入列表并建堆
|
||||
# 时间复杂度为 O(n) ,而非 O(nlogn)
|
||||
min_heap: list[int] = [1, 3, 2, 5, 4]
|
||||
heapq.heapify(min_heap)
|
||||
|
@ -105,7 +105,7 @@ class MaxHeap:
|
||||
print_heap(self.max_heap)
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化大顶堆
|
||||
max_heap = MaxHeap([9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2])
|
||||
|
@ -36,7 +36,7 @@ def binary_search1(nums: list[int], target: int) -> int:
|
||||
return -1 # 未找到目标元素,返回 -1
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
target: int = 6
|
||||
nums: list[int] = [1, 3, 6, 8, 12, 15, 23, 67, 70, 92]
|
||||
|
@ -26,7 +26,7 @@ def hashing_search_linkedlist(
|
||||
return mapp.get(target, None)
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
target: int = 3
|
||||
|
||||
|
@ -29,7 +29,7 @@ def linear_search_linkedlist(head: ListNode, target: int) -> ListNode | None:
|
||||
return None # 未找到目标节点,返回 None
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
target: int = 3
|
||||
|
||||
|
@ -33,7 +33,7 @@ def bubble_sort_with_flag(nums: list[int]) -> None:
|
||||
break # 此轮冒泡未交换任何元素,直接跳出
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
nums: list[int] = [4, 1, 3, 1, 5, 2]
|
||||
bubble_sort(nums)
|
||||
|
@ -6,6 +6,7 @@ Author: Krahets (krahets@163.com)
|
||||
|
||||
|
||||
def bucket_sort(nums: list[float]) -> None:
|
||||
"""桶排序"""
|
||||
# 初始化 k = n/2 个桶,预期向每个桶分配 2 个元素
|
||||
k = len(nums) // 2
|
||||
buckets = [[] for _ in range(k)]
|
||||
|
@ -52,7 +52,7 @@ def counting_sort(nums: list[int]) -> None:
|
||||
nums[i] = res[i]
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
nums = [1, 0, 1, 2, 0, 4, 0, 2, 2, 4]
|
||||
|
||||
|
@ -18,7 +18,7 @@ def insertion_sort(nums: list[int]) -> None:
|
||||
nums[j + 1] = base # 2. 将 base 赋值到正确位置
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
nums: list[int] = [4, 1, 3, 1, 5, 2]
|
||||
insertion_sort(nums)
|
||||
|
@ -49,7 +49,7 @@ def merge_sort(nums: list[int], left: int, right: int) -> None:
|
||||
merge(nums, left, mid, right)
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
nums: list[int] = [7, 3, 2, 6, 0, 1, 5, 4]
|
||||
merge_sort(nums, 0, len(nums) - 1)
|
||||
|
@ -112,7 +112,7 @@ class QuickSortTailCall:
|
||||
right = pivot - 1 # 剩余待排序区间为 [left, pivot - 1]
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 快速排序
|
||||
nums: list[int] = [2, 4, 1, 0, 3, 5]
|
||||
|
@ -50,7 +50,7 @@ def radix_sort(nums: list[int]) -> None:
|
||||
exp *= 10
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 基数排序
|
||||
nums = [
|
||||
|
@ -91,7 +91,7 @@ class ArrayDeque:
|
||||
return res
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化双向队列
|
||||
deque = ArrayDeque(10)
|
||||
|
@ -59,7 +59,7 @@ class ArrayQueue:
|
||||
return res
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化队列
|
||||
queue = ArrayQueue(10)
|
||||
|
@ -39,7 +39,7 @@ class ArrayStack:
|
||||
return self.__stack
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化栈
|
||||
stack = ArrayStack()
|
||||
|
@ -6,7 +6,7 @@ Author: Peng Chen (pengchzn@gmail.com)
|
||||
|
||||
from collections import deque
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化双向队列
|
||||
deq: deque[int] = deque()
|
||||
|
@ -112,7 +112,7 @@ class LinkedListDeque:
|
||||
return res
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化双向队列
|
||||
deque = LinkedListDeque()
|
||||
|
@ -66,7 +66,7 @@ class LinkedListQueue:
|
||||
return queue
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化队列
|
||||
queue = LinkedListQueue()
|
||||
|
@ -58,7 +58,7 @@ class LinkedListStack:
|
||||
return arr
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化栈
|
||||
stack = LinkedListStack()
|
||||
|
@ -6,7 +6,7 @@ Author: Peng Chen (pengchzn@gmail.com)
|
||||
|
||||
from collections import deque
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化队列
|
||||
# 在 Python 中,我们一般将双向队列类 deque 看作队列使用
|
||||
|
@ -4,7 +4,7 @@ Created Time: 2022-11-29
|
||||
Author: Peng Chen (pengchzn@gmail.com)
|
||||
"""
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化栈
|
||||
# Python 没有内置的栈类,可以把 list 当作栈来使用
|
||||
|
@ -173,7 +173,7 @@ class AVLTree:
|
||||
return cur
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
|
||||
def test_insert(tree: AVLTree, val: int):
|
||||
|
@ -136,7 +136,7 @@ class BinarySearchTree:
|
||||
return root
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化二叉搜索树
|
||||
nums = list(range(1, 16)) # [1, 2, ..., 15]
|
||||
|
@ -10,7 +10,7 @@ sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from modules import *
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化二叉树
|
||||
# 初始化节点
|
||||
|
@ -28,7 +28,7 @@ def level_order(root: TreeNode | None) -> list[int]:
|
||||
return res
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化二叉树
|
||||
# 这里借助了一个从数组直接生成二叉树的函数
|
||||
|
@ -40,7 +40,7 @@ def post_order(root: TreeNode | None) -> None:
|
||||
res.append(root.val)
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化二叉树
|
||||
# 这里借助了一个从数组直接生成二叉树的函数
|
||||
|
@ -29,7 +29,7 @@
|
||||
=== "Python"
|
||||
|
||||
```python title="array.py"
|
||||
""" 初始化数组 """
|
||||
# 初始化数组
|
||||
arr: List[int] = [0] * 5 # [ 0, 0, 0, 0, 0 ]
|
||||
nums: List[int] = [1, 3, 2, 5, 4]
|
||||
```
|
||||
@ -293,7 +293,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
[class]{}-[func]{insert}
|
||||
```
|
||||
|
||||
删除元素也是类似,如果我们想要删除索引 $i$ 处的元素,则需要把索引 $i$ 之后的元素都向前移动一位。值得注意的是,删除元素后,原先末尾的元素变得“无意义”了,我们无需特意去修改它。
|
||||
删除元素也类似,如果我们想要删除索引 $i$ 处的元素,则需要把索引 $i$ 之后的元素都向前移动一位。值得注意的是,删除元素后,原先末尾的元素变得“无意义”了,我们无需特意去修改它。
|
||||
|
||||
![数组删除元素](array.assets/array_remove_element.png)
|
||||
|
||||
|
@ -1,10 +1,10 @@
|
||||
# 链表
|
||||
|
||||
内存空间是所有程序的公共资源,排除已被占用的内存空间,空闲内存空间通常散落在内存各处。在上一节中,我们提到存储数组的内存空间必须是连续的,而当我们需要申请一个非常大的数组时,空闲内存中可能没有这么大的连续空间。
|
||||
内存空间是所有程序的公共资源,排除已被占用的内存空间,空闲内存空间通常散落在内存各处。在上一节中,我们提到存储数组的内存空间必须是连续的,而当我们需要申请一个非常大的数组时,空闲内存中可能没有这么大的连续空间。与数组相比,链表更具灵活性,它可以被存储在非连续的内存空间中。
|
||||
|
||||
与数组相比,链表更具灵活性,因为它可以存储在非连续的内存空间。「链表 Linked List」是一种线性数据结构,其每个元素都是一个节点对象,各个节点之间通过指针连接,从当前节点通过指针可以访问到下一个节点。由于指针记录了下个节点的内存地址,因此无需保证内存地址的连续性,从而可以将各个节点分散存储在内存各处。
|
||||
「链表 Linked List」是一种线性数据结构,其每个元素都是一个节点对象,各个节点之间通过指针连接,从当前节点通过指针可以访问到下一个节点。**由于指针记录了下个节点的内存地址,因此无需保证内存地址的连续性**,从而可以将各个节点分散存储在内存各处。
|
||||
|
||||
链表「节点 Node」包含两项数据,一是节点「值 Value」,二是指向下一节点的「指针 Pointer」,或称指向下一节点的「引用 Reference」。
|
||||
链表「节点 Node」包含两项数据,一是节点「值 Value」,二是指向下一节点的「指针 Pointer」,或称「引用 Reference」。
|
||||
|
||||
![链表定义与存储方式](linked_list.assets/linkedlist_definition.png)
|
||||
|
||||
@ -33,8 +33,8 @@
|
||||
=== "Python"
|
||||
|
||||
```python title=""
|
||||
""" 链表节点类 """
|
||||
class ListNode:
|
||||
"""链表节点类"""
|
||||
def __init__(self, val: int):
|
||||
self.val: int = val # 节点值
|
||||
self.next: Optional[ListNode] = None # 指向下一节点的指针(引用)
|
||||
@ -201,7 +201,7 @@
|
||||
=== "Python"
|
||||
|
||||
```python title="linked_list.py"
|
||||
""" 初始化链表 1 -> 3 -> 2 -> 5 -> 4 """
|
||||
# 初始化链表 1 -> 3 -> 2 -> 5 -> 4
|
||||
# 初始化各个节点
|
||||
n0 = ListNode(1)
|
||||
n1 = ListNode(3)
|
||||
@ -629,8 +629,8 @@
|
||||
=== "Python"
|
||||
|
||||
```python title=""
|
||||
""" 双向链表节点类 """
|
||||
class ListNode:
|
||||
"""双向链表节点类"""
|
||||
def __init__(self, val: int):
|
||||
self.val: int = val # 节点值
|
||||
self.next: Optional[ListNode] = None # 指向后继节点的指针(引用)
|
||||
|
@ -33,7 +33,7 @@
|
||||
=== "Python"
|
||||
|
||||
```python title="list.py"
|
||||
""" 初始化列表 """
|
||||
# 初始化列表
|
||||
# 无初始值
|
||||
list1: List[int] = []
|
||||
# 有初始值
|
||||
@ -131,10 +131,10 @@
|
||||
=== "Python"
|
||||
|
||||
```python title="list.py"
|
||||
""" 访问元素 """
|
||||
# 访问元素
|
||||
num: int = list[1] # 访问索引 1 处的元素
|
||||
|
||||
""" 更新元素 """
|
||||
# 更新元素
|
||||
list[1] = 0 # 将索引 1 处的元素更新为 0
|
||||
```
|
||||
|
||||
@ -249,20 +249,20 @@
|
||||
=== "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 处的元素
|
||||
```
|
||||
|
||||
@ -429,12 +429,12 @@
|
||||
=== "Python"
|
||||
|
||||
```python title="list.py"
|
||||
""" 通过索引遍历列表 """
|
||||
# 通过索引遍历列表
|
||||
count: int = 0
|
||||
for i in range(len(list)):
|
||||
count += 1
|
||||
|
||||
""" 直接遍历列表元素 """
|
||||
# 直接遍历列表元素
|
||||
count: int = 0
|
||||
for n in list:
|
||||
count += 1
|
||||
@ -567,7 +567,7 @@
|
||||
=== "Python"
|
||||
|
||||
```python title="list.py"
|
||||
""" 拼接两个列表 """
|
||||
# 拼接两个列表
|
||||
list1: List[int] = [6, 8, 7, 10, 9]
|
||||
list += list1 # 将列表 list1 拼接到 list 之后
|
||||
```
|
||||
@ -647,7 +647,7 @@
|
||||
=== "Python"
|
||||
|
||||
```python title="list.py"
|
||||
""" 排序列表 """
|
||||
# 排序列表
|
||||
list.sort() # 排序后,列表元素从小到大排列
|
||||
```
|
||||
|
||||
|
@ -75,14 +75,14 @@
|
||||
=== "Python"
|
||||
|
||||
```python title=""
|
||||
""" 类 """
|
||||
class Node:
|
||||
"""类"""
|
||||
def __init__(self, x: int):
|
||||
self.val: int = x # 节点值
|
||||
self.next: Optional[Node] = None # 指向下一节点的指针(引用)
|
||||
|
||||
""" 函数 """
|
||||
def function() -> int:
|
||||
"""函数"""
|
||||
# do something...
|
||||
return 0
|
||||
|
||||
@ -413,13 +413,13 @@
|
||||
# do something
|
||||
return 0
|
||||
|
||||
""" 循环 O(1) """
|
||||
def loop(n: int) -> None:
|
||||
"""循环 O(1)"""
|
||||
for _ in range(n):
|
||||
function()
|
||||
|
||||
""" 递归 O(n) """
|
||||
def recur(n: int) -> int:
|
||||
"""递归 O(n)"""
|
||||
if n == 1: return
|
||||
return recur(n - 1)
|
||||
```
|
||||
|
@ -132,7 +132,7 @@ $$
|
||||
=== "Python"
|
||||
|
||||
```python title=""
|
||||
""" Python 的 list 可以自由存储各种基本数据类型和对象 """
|
||||
# Python 的 list 可以自由存储各种基本数据类型和对象
|
||||
list = [0, 0.0, 'a', False]
|
||||
```
|
||||
|
||||
|
@ -80,10 +80,10 @@
|
||||
=== "Python"
|
||||
|
||||
```python title="hash_map.py"
|
||||
""" 初始化哈希表 """
|
||||
# 初始化哈希表
|
||||
mapp: Dict = {}
|
||||
|
||||
""" 添加操作 """
|
||||
# 添加操作
|
||||
# 在哈希表中添加键值对 (key, value)
|
||||
mapp[12836] = "小哈"
|
||||
mapp[15937] = "小啰"
|
||||
@ -91,11 +91,11 @@
|
||||
mapp[13276] = "小法"
|
||||
mapp[10583] = "小鸭"
|
||||
|
||||
""" 查询操作 """
|
||||
# 查询操作
|
||||
# 向哈希表输入键 key ,得到值 value
|
||||
name: str = mapp[15937]
|
||||
|
||||
""" 删除操作 """
|
||||
# 删除操作
|
||||
# 在哈希表中删除键值对 (key, value)
|
||||
mapp.pop(10583)
|
||||
```
|
||||
@ -271,7 +271,7 @@
|
||||
=== "Python"
|
||||
|
||||
```python title="hash_map.py"
|
||||
""" 遍历哈希表 """
|
||||
# 遍历哈希表
|
||||
# 遍历键值对 key->value
|
||||
for key, value in mapp.items():
|
||||
print(key, "->", value)
|
||||
|
@ -125,17 +125,18 @@
|
||||
# Python 的 heapq 模块默认实现小顶堆
|
||||
# 考虑将“元素取负”后再入堆,这样就可以将大小关系颠倒,从而实现大顶堆
|
||||
# 在本示例中,flag = 1 时对应小顶堆,flag = -1 时对应大顶堆
|
||||
""" 元素入堆 """
|
||||
|
||||
# 元素入堆
|
||||
heapq.heappush(max_heap, flag * 1)
|
||||
heapq.heappush(max_heap, flag * 3)
|
||||
heapq.heappush(max_heap, flag * 2)
|
||||
heapq.heappush(max_heap, flag * 5)
|
||||
heapq.heappush(max_heap, flag * 4)
|
||||
|
||||
""" 获取堆顶元素 """
|
||||
# 获取堆顶元素
|
||||
peek: int = flag * max_heap[0] # 5
|
||||
|
||||
""" 堆顶元素出堆 """
|
||||
# 堆顶元素出堆
|
||||
# 出堆元素会形成一个从大到小的序列
|
||||
val = flag * heapq.heappop(max_heap) # 5
|
||||
val = flag * heapq.heappop(max_heap) # 4
|
||||
@ -143,13 +144,13 @@
|
||||
val = flag * heapq.heappop(max_heap) # 2
|
||||
val = flag * heapq.heappop(max_heap) # 1
|
||||
|
||||
""" 获取堆大小 """
|
||||
# 获取堆大小
|
||||
size: int = len(max_heap)
|
||||
|
||||
""" 判断堆是否为空 """
|
||||
# 判断堆是否为空
|
||||
is_empty: bool = not max_heap
|
||||
|
||||
""" 输入列表并建堆 """
|
||||
# 输入列表并建堆
|
||||
min_heap: List[int] = [1, 3, 2, 5, 4]
|
||||
heapq.heapify(min_heap)
|
||||
```
|
||||
|
@ -55,7 +55,7 @@
|
||||
=== "Python"
|
||||
|
||||
```python title=""
|
||||
""" 标题注释,用于标注函数、类、测试样例等 """
|
||||
"""标题注释,用于标注函数、类、测试样例等"""
|
||||
|
||||
# 内容注释,用于详解代码
|
||||
|
||||
|
@ -82,28 +82,28 @@
|
||||
=== "Python"
|
||||
|
||||
```python title="deque.py"
|
||||
""" 初始化双向队列 """
|
||||
# 初始化双向队列
|
||||
deque: Deque[int] = collections.deque()
|
||||
|
||||
""" 元素入队 """
|
||||
# 元素入队
|
||||
deque.append(2) # 添加至队尾
|
||||
deque.append(5)
|
||||
deque.append(4)
|
||||
deque.appendleft(3) # 添加至队首
|
||||
deque.appendleft(1)
|
||||
|
||||
""" 访问元素 """
|
||||
# 访问元素
|
||||
front: int = deque[0] # 队首元素
|
||||
rear: int = deque[-1] # 队尾元素
|
||||
|
||||
""" 元素出队 """
|
||||
# 元素出队
|
||||
pop_front: int = deque.popleft() # 队首元素出队
|
||||
pop_rear: int = deque.pop() # 队尾元素出队
|
||||
|
||||
""" 获取双向队列的长度 """
|
||||
# 获取双向队列的长度
|
||||
size: int = len(deque)
|
||||
|
||||
""" 判断双向队列是否为空 """
|
||||
# 判断双向队列是否为空
|
||||
is_empty: bool = len(deque) == 0
|
||||
```
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
# 队列
|
||||
|
||||
「队列 Queue」是一种遵循「先入先出 first in, first out」数据操作规则的线性数据结构。顾名思义,队列模拟的是排队现象,即外面的人不断加入队列尾部,而处于队列头部的人不断地离开。
|
||||
「队列 Queue」是一种遵循先入先出(first in, first out)数据操作规则的线性数据结构。顾名思义,队列模拟的是排队现象,即外面的人不断加入队列尾部,而处于队列头部的人不断地离开。
|
||||
|
||||
我们将队列头部称为「队首」,队列尾部称为「队尾」,将把元素加入队尾的操作称为「入队」,删除队首元素的操作称为「出队」。
|
||||
|
||||
@ -77,28 +77,28 @@
|
||||
=== "Python"
|
||||
|
||||
```python title="queue.py"
|
||||
""" 初始化队列 """
|
||||
# 初始化队列
|
||||
# 在 Python 中,我们一般将双向队列类 deque 看作队列使用
|
||||
# 虽然 queue.Queue() 是纯正的队列类,但不太好用,因此不建议
|
||||
que: Deque[int] = collections.deque()
|
||||
|
||||
""" 元素入队 """
|
||||
# 元素入队
|
||||
que.append(1)
|
||||
que.append(3)
|
||||
que.append(2)
|
||||
que.append(5)
|
||||
que.append(4)
|
||||
|
||||
""" 访问队首元素 """
|
||||
# 访问队首元素
|
||||
front: int = que[0];
|
||||
|
||||
""" 元素出队 """
|
||||
# 元素出队
|
||||
pop: int = que.popleft()
|
||||
|
||||
""" 获取队列的长度 """
|
||||
# 获取队列的长度
|
||||
size: int = len(que)
|
||||
|
||||
""" 判断队列是否为空 """
|
||||
# 判断队列是否为空
|
||||
is_empty: bool = len(que) == 0
|
||||
```
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
# 栈
|
||||
|
||||
「栈 Stack」是一种遵循「先入后出 first in, last out」数据操作规则的线性数据结构。我们可以将栈类比为放在桌面上的一摞盘子,如果需要拿出底部的盘子,则需要先将上面的盘子依次取出。
|
||||
「栈 Stack」是一种遵循先入后出(first in, last out)数据操作规则的线性数据结构。我们可以将栈类比为放在桌面上的一摞盘子,如果需要拿出底部的盘子,则需要先将上面的盘子依次取出。
|
||||
|
||||
“盘子”是一种形象比喻,我们将盘子替换为任意一种元素(例如整数、字符、对象等),就得到了栈数据结构。
|
||||
|
||||
@ -79,27 +79,27 @@
|
||||
=== "Python"
|
||||
|
||||
```python title="stack.py"
|
||||
""" 初始化栈 """
|
||||
# 初始化栈
|
||||
# Python 没有内置的栈类,可以把 List 当作栈来使用
|
||||
stack: List[int] = []
|
||||
|
||||
""" 元素入栈 """
|
||||
# 元素入栈
|
||||
stack.append(1)
|
||||
stack.append(3)
|
||||
stack.append(2)
|
||||
stack.append(5)
|
||||
stack.append(4)
|
||||
|
||||
""" 访问栈顶元素 """
|
||||
# 访问栈顶元素
|
||||
peek: int = stack[-1]
|
||||
|
||||
""" 元素出栈 """
|
||||
# 元素出栈
|
||||
pop: int = stack.pop()
|
||||
|
||||
""" 获取栈的长度 """
|
||||
# 获取栈的长度
|
||||
size: int = len(stack)
|
||||
|
||||
""" 判断是否为空 """
|
||||
# 判断是否为空
|
||||
is_empty: bool = len(stack) == 0
|
||||
```
|
||||
|
||||
|
@ -52,8 +52,8 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
=== "Python"
|
||||
|
||||
```python title=""
|
||||
""" AVL 树节点类 """
|
||||
class TreeNode:
|
||||
"""AVL 树节点类"""
|
||||
def __init__(self, val: int):
|
||||
self.val: int = val # 节点值
|
||||
self.height: int = 0 # 节点高度
|
||||
|
@ -29,8 +29,8 @@
|
||||
=== "Python"
|
||||
|
||||
```python title=""
|
||||
""" 二叉树节点类 """
|
||||
class TreeNode:
|
||||
"""二叉树节点类"""
|
||||
def __init__(self, val: int):
|
||||
self.val: int = val # 节点值
|
||||
self.left: Optional[TreeNode] = None # 左子节点指针
|
||||
@ -188,7 +188,7 @@
|
||||
=== "Python"
|
||||
|
||||
```python title="binary_tree.py"
|
||||
""" 初始化二叉树 """
|
||||
# 初始化二叉树
|
||||
# 初始化节点
|
||||
n1 = TreeNode(val=1)
|
||||
n2 = TreeNode(val=2)
|
||||
@ -328,7 +328,7 @@
|
||||
=== "Python"
|
||||
|
||||
```python title="binary_tree.py"
|
||||
""" 插入与删除节点 """
|
||||
# 插入与删除节点
|
||||
p = TreeNode(0)
|
||||
# 在 n1 -> n2 中间插入节点 P
|
||||
n1.left = p
|
||||
@ -502,7 +502,7 @@
|
||||
=== "Python"
|
||||
|
||||
```python title=""
|
||||
""" 二叉树的数组表示 """
|
||||
# 二叉树的数组表示
|
||||
# 直接使用 None 来表示空位
|
||||
tree = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
|
||||
```
|
||||
|
Loading…
Reference in New Issue
Block a user