File tree 3 files changed +126
-0
lines changed
lcci/03.05.Sort of Stacks
3 files changed +126
-0
lines changed Original file line number Diff line number Diff line change @@ -314,6 +314,49 @@ impl SortedStack {
314
314
*/
315
315
```
316
316
317
+ ``` swift
318
+ class SortedStack {
319
+ private var stk: [Int ] = []
320
+
321
+ init () {}
322
+
323
+ func push (_ val : Int ) {
324
+ var temp: [Int ] = []
325
+ while let top = stk.last , top < val {
326
+ temp.append (stk.removeLast ())
327
+ }
328
+ stk.append (val)
329
+ while let last = temp.popLast () {
330
+ stk.append (last)
331
+ }
332
+ }
333
+
334
+ func pop () {
335
+ if ! isEmpty () {
336
+ stk.removeLast ()
337
+ }
338
+ }
339
+
340
+ func peek () -> Int {
341
+ return isEmpty () ? -1 : stk.last !
342
+ }
343
+
344
+ func isEmpty () -> Bool {
345
+ return stk.isEmpty
346
+ }
347
+ }
348
+
349
+ /**
350
+ * Your SortedStack object will be instantiated and called as such:
351
+ * let obj = new SortedStack();
352
+ * obj.push(val);
353
+ * obj.pop();
354
+ * let param_3 = obj.peek();
355
+ * var myVar: Bool;
356
+ * myVar = obj.isEmpty();
357
+ */
358
+ ```
359
+
317
360
<!-- tabs:end -->
318
361
319
362
<!-- end -->
Original file line number Diff line number Diff line change @@ -327,6 +327,49 @@ impl SortedStack {
327
327
*/
328
328
```
329
329
330
+ ``` swift
331
+ class SortedStack {
332
+ private var stk: [Int ] = []
333
+
334
+ init () {}
335
+
336
+ func push (_ val : Int ) {
337
+ var temp: [Int ] = []
338
+ while let top = stk.last , top < val {
339
+ temp.append (stk.removeLast ())
340
+ }
341
+ stk.append (val)
342
+ while let last = temp.popLast () {
343
+ stk.append (last)
344
+ }
345
+ }
346
+
347
+ func pop () {
348
+ if ! isEmpty () {
349
+ stk.removeLast ()
350
+ }
351
+ }
352
+
353
+ func peek () -> Int {
354
+ return isEmpty () ? -1 : stk.last !
355
+ }
356
+
357
+ func isEmpty () -> Bool {
358
+ return stk.isEmpty
359
+ }
360
+ }
361
+
362
+ /**
363
+ * Your SortedStack object will be instantiated and called as such:
364
+ * let obj = new SortedStack();
365
+ * obj.push(val);
366
+ * obj.pop();
367
+ * let param_3 = obj.peek();
368
+ * var myVar: Bool;
369
+ * myVar = obj.isEmpty();
370
+ */
371
+ ```
372
+
330
373
<!-- tabs:end -->
331
374
332
375
<!-- end -->
Original file line number Diff line number Diff line change
1
+ class SortedStack {
2
+ private var stk : [ Int ] = [ ]
3
+
4
+ init ( ) { }
5
+
6
+ func push( _ val: Int ) {
7
+ var temp : [ Int ] = [ ]
8
+ while let top = stk. last, top < val {
9
+ temp. append ( stk. removeLast ( ) )
10
+ }
11
+ stk. append ( val)
12
+ while let last = temp. popLast ( ) {
13
+ stk. append ( last)
14
+ }
15
+ }
16
+
17
+ func pop( ) {
18
+ if !isEmpty( ) {
19
+ stk. removeLast ( )
20
+ }
21
+ }
22
+
23
+ func peek( ) -> Int {
24
+ return isEmpty ( ) ? - 1 : stk. last!
25
+ }
26
+
27
+ func isEmpty( ) -> Bool {
28
+ return stk. isEmpty
29
+ }
30
+ }
31
+
32
+ /**
33
+ * Your SortedStack object will be instantiated and called as such:
34
+ * let obj = new SortedStack();
35
+ * obj.push(val);
36
+ * obj.pop();
37
+ * let param_3 = obj.peek();
38
+ * var myVar: Bool;
39
+ * myVar = obj.isEmpty();
40
+ */
You can’t perform that action at this time.
0 commit comments