赞
踩
- import heapq
- def merge(lists):
- heap = []
- for i,l in enumerate(lists):
- heap.append((l.pop(0),i))
- heapq.heapify(heap)
- result = []
- while heap:
- val,ids = heapq.heappop(heap)
- result.append(val)
- if lists[ids]:
- heapq.heappush(heap,(lists[ids].pop(0),ids))
- return result
时间复杂度为nlogk,n为三个列表的总长度。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。