196 使用堆排序列表为升序
使用 heapq 模块,首先对列表建堆,默认建立小根堆,调用len(nums) 次heappop:
import heapq as hq
nums_list = [18, 14, 10, 9, 8, 7, 9, 3, 2, 4, 1]
hq.heapify(nums_list)
s_result = [hq.heappop(nums_list) for _ in range(len(nums_list))]
print(s_result)
使用 heapq 模块,首先对列表建堆,默认建立小根堆,调用len(nums) 次heappop:
import heapq as hq
nums_list = [18, 14, 10, 9, 8, 7, 9, 3, 2, 4, 1]
hq.heapify(nums_list)
s_result = [hq.heappop(nums_list) for _ in range(len(nums_list))]
print(s_result)