mirror of
https://github.com/krahets/hello-algo.git
synced 2026-05-12 03:27:44 +08:00
Unify the comment style of python codes
This commit is contained in:
@@ -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)
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user