@@ -53,6 +53,8 @@ smallestInfiniteSet.popSmallest(); // 返回 5 ,并将其从集合中移除。
53
53
54
54
** 方法一:哈希表**
55
55
56
+ ** 方法二:哈希表 + 优先队列(小根堆)**
57
+
56
58
<!-- tabs:start -->
57
59
58
60
### ** Python3**
@@ -76,6 +78,31 @@ class SmallestInfiniteSet:
76
78
self .black.discard(num)
77
79
78
80
81
+ # Your SmallestInfiniteSet object will be instantiated and called as such:
82
+ # obj = SmallestInfiniteSet()
83
+ # param_1 = obj.popSmallest()
84
+ # obj.addBack(num)
85
+ ```
86
+
87
+ ``` python
88
+ class SmallestInfiniteSet :
89
+
90
+ def __init__ (self ):
91
+ self .h = list (range (1 , 1010 ))
92
+ self .s = set (self .h)
93
+ heapify(self .h)
94
+
95
+ def popSmallest (self ) -> int :
96
+ ans = heappop(self .h)
97
+ self .s.discard(ans)
98
+ return ans
99
+
100
+ def addBack (self , num : int ) -> None :
101
+ if num not in self .s:
102
+ self .s.add(num)
103
+ heappush(self .h, num)
104
+
105
+
79
106
# Your SmallestInfiniteSet object will be instantiated and called as such:
80
107
# obj = SmallestInfiniteSet()
81
108
# param_1 = obj.popSmallest()
@@ -114,6 +141,40 @@ class SmallestInfiniteSet {
114
141
*/
115
142
```
116
143
144
+ ``` java
145
+ class SmallestInfiniteSet {
146
+ private PriorityQueue<Integer > pq = new PriorityQueue<> ();
147
+ private Set<Integer > s = new HashSet<> ();
148
+
149
+ public SmallestInfiniteSet () {
150
+ for (int i = 1 ; i < 1010 ; ++ i) {
151
+ pq. offer(i);
152
+ s. add(i);
153
+ }
154
+ }
155
+
156
+ public int popSmallest () {
157
+ int ans = pq. poll();
158
+ s. remove(ans);
159
+ return ans;
160
+ }
161
+
162
+ public void addBack (int num ) {
163
+ if (! s. contains(num)) {
164
+ s. add(num);
165
+ pq. offer(num);
166
+ }
167
+ }
168
+ }
169
+
170
+ /**
171
+ * Your SmallestInfiniteSet object will be instantiated and called as such:
172
+ * SmallestInfiniteSet obj = new SmallestInfiniteSet();
173
+ * int param_1 = obj.popSmallest();
174
+ * obj.addBack(num);
175
+ */
176
+ ```
177
+
117
178
### ** C++**
118
179
119
180
``` cpp
@@ -145,6 +206,44 @@ public:
145
206
*/
146
207
```
147
208
209
+ ``` cpp
210
+ class SmallestInfiniteSet {
211
+ public:
212
+ priority_queue<int, vector<int >, greater<int >> pq;
213
+ unordered_set<int > s;
214
+
215
+ SmallestInfiniteSet() {
216
+ for (int i = 1; i < 1010; ++i)
217
+ {
218
+ pq.push(i);
219
+ s.insert(i);
220
+ }
221
+ }
222
+
223
+ int popSmallest () {
224
+ int ans = pq.top();
225
+ pq.pop();
226
+ s.erase(ans);
227
+ return ans;
228
+ }
229
+
230
+ void addBack(int num) {
231
+ if (!s.count(num))
232
+ {
233
+ s.insert(num);
234
+ pq.push(num);
235
+ }
236
+ }
237
+ };
238
+
239
+ /* *
240
+ * Your SmallestInfiniteSet object will be instantiated and called as such:
241
+ * SmallestInfiniteSet* obj = new SmallestInfiniteSet();
242
+ * int param_1 = obj->popSmallest();
243
+ * obj->addBack(num);
244
+ */
245
+ ```
246
+
148
247
### ** Go**
149
248
150
249
``` go
@@ -177,6 +276,54 @@ func (this *SmallestInfiniteSet) AddBack(num int) {
177
276
*/
178
277
```
179
278
279
+ ``` go
280
+ type SmallestInfiniteSet struct {
281
+ h *hp
282
+ s map [int ]bool
283
+ }
284
+
285
+ func Constructor () SmallestInfiniteSet {
286
+ h := &hp{}
287
+ s := map [int ]bool {}
288
+ for i := 1 ; i < 1010 ; i++ {
289
+ s[i] = true
290
+ h.push (i)
291
+ }
292
+ return SmallestInfiniteSet{h, s}
293
+ }
294
+
295
+ func (this *SmallestInfiniteSet ) PopSmallest () int {
296
+ ans := this.h .pop ()
297
+ this.s [ans] = false
298
+ return ans
299
+ }
300
+
301
+ func (this *SmallestInfiniteSet ) AddBack (num int ) {
302
+ if !this.s [num] {
303
+ this.s [num] = true
304
+ this.h .push (num)
305
+ }
306
+ }
307
+
308
+ type hp []int
309
+
310
+ func (h hp ) Len () int { return len (h) }
311
+ func (h hp ) Less (i , j int ) bool { return h[i] < h[j] }
312
+ func (h hp ) Swap (i , j int ) { h[i], h[j] = h[j], h[i] }
313
+ func (h *hp ) Push (v interface {}) { *h = append (*h, v.(int )) }
314
+ func (h *hp ) Pop () (v interface {}) { a := *h; *h, v = a[:len (a)-1 ], a[len (a)-1 ]; return }
315
+ func (h *hp ) push (v int ) { heap.Push (h, v) }
316
+ func (h *hp ) pop () int { return heap.Pop (h).(int ) }
317
+ func (h *hp ) top () int { a := *h; return a[0 ] }
318
+
319
+ /* *
320
+ * Your SmallestInfiniteSet object will be instantiated and called as such:
321
+ * obj := Constructor();
322
+ * param_1 := obj.PopSmallest();
323
+ * obj.AddBack(num);
324
+ */
325
+ ```
326
+
180
327
### ** TypeScript**
181
328
182
329
``` ts
0 commit comments