题解 | 字符串优先队列
字符串优先队列
https://www.nowcoder.com/practice/7f3c2ebfc3be442897393f7da5d021c8
import heapq
# 使用Python的heapq模块实现字典序最小的字符串堆
s = []
def insertValue(x):
# TODO: 实现插入操作
heapq.heappush(s,x)
pass
def deleteValue():
heapq.heappop(s)
# TODO: 实现删除操作
pass
def getTop():
return s[0]
# TODO: 返回字典序最小的字符串
pass
if __name__ == "__main__":
q = int(input())
for _ in range(q):
line = input().split()
op = int(line[0])
if op == 1:
x = line[1]
insertValue(x)
elif op == 2:
print(getTop())
elif op == 3:
deleteValue()
查看16道真题和解析