模拟优先队列,实现push、pop和初始化操作,用python的heapq
1 #2336
2 #Runtime: 223 ms (Beats 35.71%)
3 #Memory: 14.2 MB (Beats 14.29%)
4
5 class SmallestInfiniteSet(object):
6
7 def __init__(self):
8 self.hq = []
9 for i in range(1, 1001):
10 heapq.heappush(self.hq, i)
11
12 def popSmallest(self):
13 """
14 :rtype: int
15 """
16 return heapq.heappop(self.hq)
17
18
19 def addBack(self, num):
20 """
21 :type num: int
22 :rtype: None
23 """
24 if num not in self.hq:
25 heapq.heappush(self.hq, num)
26
27
28 # Your SmallestInfiniteSet object will be instantiated and called as such:
29 # obj = SmallestInfiniteSet()
30 # param_1 = obj.popSmallest()
31 # obj.addBack(num)