Unify the comment style of python codes

This commit is contained in:
krahets
2023-04-09 05:30:02 +08:00
parent 5ddcb60825
commit 10e2180013
53 changed files with 109 additions and 106 deletions

View File

@@ -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)
```