File tree 3 files changed +135
-0
lines changed
lcci/03.04.Implement Queue using Stacks
3 files changed +135
-0
lines changed Original file line number Diff line number Diff line change @@ -299,6 +299,52 @@ impl MyQueue {
299
299
*/
300
300
```
301
301
302
+ ``` swift
303
+ class MyQueue {
304
+ private var stk1: [Int ] = []
305
+ private var stk2: [Int ] = []
306
+
307
+ init () {}
308
+
309
+ func push (_ x : Int ) {
310
+ stk1.append (x)
311
+ }
312
+
313
+ @discardableResult
314
+ func pop () -> Int {
315
+ move ()
316
+ return stk2.removeLast ()
317
+ }
318
+
319
+ func peek () -> Int {
320
+ move ()
321
+ return stk2.last !
322
+ }
323
+
324
+ func empty () -> Bool {
325
+ return stk1.isEmpty && stk2.isEmpty
326
+ }
327
+
328
+ private func move () {
329
+ if stk2.isEmpty {
330
+ while ! stk1.isEmpty {
331
+ stk2.append (stk1.removeLast ())
332
+ }
333
+ }
334
+ }
335
+ }
336
+
337
+ /**
338
+ * Your MyQueue object will be instantiated and called as such:
339
+ * let obj = new MyQueue();
340
+ * obj.push(x);
341
+ * let param_2 = obj.pop();
342
+ * let param_3 = obj.peek();
343
+ * var myValue : Bool
344
+ * myValue = obj.empty();
345
+ */
346
+ ```
347
+
302
348
<!-- tabs:end -->
303
349
304
350
<!-- end -->
Original file line number Diff line number Diff line change @@ -330,6 +330,52 @@ impl MyQueue {
330
330
*/
331
331
```
332
332
333
+ ``` swift
334
+ class MyQueue {
335
+ private var stk1: [Int ] = []
336
+ private var stk2: [Int ] = []
337
+
338
+ init () {}
339
+
340
+ func push (_ x : Int ) {
341
+ stk1.append (x)
342
+ }
343
+
344
+ @discardableResult
345
+ func pop () -> Int {
346
+ move ()
347
+ return stk2.removeLast ()
348
+ }
349
+
350
+ func peek () -> Int {
351
+ move ()
352
+ return stk2.last !
353
+ }
354
+
355
+ func empty () -> Bool {
356
+ return stk1.isEmpty && stk2.isEmpty
357
+ }
358
+
359
+ private func move () {
360
+ if stk2.isEmpty {
361
+ while ! stk1.isEmpty {
362
+ stk2.append (stk1.removeLast ())
363
+ }
364
+ }
365
+ }
366
+ }
367
+
368
+ /**
369
+ * Your MyQueue object will be instantiated and called as such:
370
+ * let obj = new MyQueue();
371
+ * obj.push(x);
372
+ * let param_2 = obj.pop();
373
+ * let param_3 = obj.peek();
374
+ * var myValue : Bool
375
+ * myValue = obj.empty();
376
+ */
377
+ ```
378
+
333
379
<!-- tabs:end -->
334
380
335
381
<!-- end -->
Original file line number Diff line number Diff line change
1
+ class MyQueue {
2
+ private var stk1 : [ Int ] = [ ]
3
+ private var stk2 : [ Int ] = [ ]
4
+
5
+ init ( ) { }
6
+
7
+ func push( _ x: Int ) {
8
+ stk1. append ( x)
9
+ }
10
+
11
+ @discardableResult
12
+ func pop( ) -> Int {
13
+ move ( )
14
+ return stk2. removeLast ( )
15
+ }
16
+
17
+ func peek( ) -> Int {
18
+ move ( )
19
+ return stk2. last!
20
+ }
21
+
22
+ func empty( ) -> Bool {
23
+ return stk1. isEmpty && stk2. isEmpty
24
+ }
25
+
26
+ private func move( ) {
27
+ if stk2. isEmpty {
28
+ while !stk1. isEmpty {
29
+ stk2. append ( stk1. removeLast ( ) )
30
+ }
31
+ }
32
+ }
33
+ }
34
+
35
+ /**
36
+ * Your MyQueue object will be instantiated and called as such:
37
+ * let obj = new MyQueue();
38
+ * obj.push(x);
39
+ * let param_2 = obj.pop();
40
+ * let param_3 = obj.peek();
41
+ * var myValue : Bool
42
+ * myValue = obj.empty();
43
+ */
You can’t perform that action at this time.
0 commit comments