This commit is contained in:
krahets
2023-04-09 05:30:47 +08:00
parent 37f11aff68
commit 2289822dfd
13 changed files with 59 additions and 58 deletions
+7 -6
View File
@@ -131,17 +131,18 @@ comments: true
# 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
@@ -149,13 +150,13 @@ comments: true
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)
```