File tree 3 files changed +132
-0
lines changed
lcci/03.03.Stack of Plates
3 files changed +132
-0
lines changed Original file line number Diff line number Diff line change @@ -258,6 +258,51 @@ class StackOfPlates {
258
258
*/
259
259
```
260
260
261
+ ``` swift
262
+ class StackOfPlates {
263
+ private var stacks: [[Int ]]
264
+ private var cap: Int
265
+
266
+ init (_ cap : Int ) {
267
+ self .cap = cap
268
+ self .stacks = []
269
+ }
270
+
271
+ func push (_ val : Int ) {
272
+ if cap == 0 {
273
+ return
274
+ }
275
+ if stacks.isEmpty || stacks.last ! .count >= cap {
276
+ stacks.append ([])
277
+ }
278
+ stacks[stacks.count - 1 ].append (val)
279
+ }
280
+
281
+ func pop () -> Int {
282
+ return popAt (stacks.count - 1 )
283
+ }
284
+
285
+ func popAt (_ index : Int ) -> Int {
286
+ guard index >= 0 , index < stacks.count , ! stacks[index].isEmpty else {
287
+ return -1
288
+ }
289
+ let value = stacks[index].removeLast ()
290
+ if stacks[index].isEmpty {
291
+ stacks.remove (at : index)
292
+ }
293
+ return value
294
+ }
295
+ }
296
+
297
+ /**
298
+ * Your StackOfPlates object will be instantiated and called as such:
299
+ * let obj = new StackOfPlates(cap);
300
+ * obj.push(val);
301
+ * let param_2 = obj.pop();
302
+ * let param_3 = obj.popAt(index);
303
+ */
304
+ ```
305
+
261
306
<!-- tabs:end -->
262
307
263
308
<!-- end -->
Original file line number Diff line number Diff line change @@ -273,6 +273,51 @@ class StackOfPlates {
273
273
*/
274
274
```
275
275
276
+ ``` swift
277
+ class StackOfPlates {
278
+ private var stacks: [[Int ]]
279
+ private var cap: Int
280
+
281
+ init (_ cap : Int ) {
282
+ self .cap = cap
283
+ self .stacks = []
284
+ }
285
+
286
+ func push (_ val : Int ) {
287
+ if cap == 0 {
288
+ return
289
+ }
290
+ if stacks.isEmpty || stacks.last ! .count >= cap {
291
+ stacks.append ([])
292
+ }
293
+ stacks[stacks.count - 1 ].append (val)
294
+ }
295
+
296
+ func pop () -> Int {
297
+ return popAt (stacks.count - 1 )
298
+ }
299
+
300
+ func popAt (_ index : Int ) -> Int {
301
+ guard index >= 0 , index < stacks.count , ! stacks[index].isEmpty else {
302
+ return -1
303
+ }
304
+ let value = stacks[index].removeLast ()
305
+ if stacks[index].isEmpty {
306
+ stacks.remove (at : index)
307
+ }
308
+ return value
309
+ }
310
+ }
311
+
312
+ /**
313
+ * Your StackOfPlates object will be instantiated and called as such:
314
+ * let obj = new StackOfPlates(cap);
315
+ * obj.push(val);
316
+ * let param_2 = obj.pop();
317
+ * let param_3 = obj.popAt(index);
318
+ */
319
+ ```
320
+
276
321
<!-- tabs:end -->
277
322
278
323
<!-- end -->
Original file line number Diff line number Diff line change
1
+ class StackOfPlates {
2
+ private var stacks : [ [ Int ] ]
3
+ private var cap : Int
4
+
5
+ init ( _ cap: Int ) {
6
+ self . cap = cap
7
+ self . stacks = [ ]
8
+ }
9
+
10
+ func push( _ val: Int ) {
11
+ if cap == 0 {
12
+ return
13
+ }
14
+ if stacks. isEmpty || stacks. last!. count >= cap {
15
+ stacks. append ( [ ] )
16
+ }
17
+ stacks [ stacks. count - 1 ] . append ( val)
18
+ }
19
+
20
+ func pop( ) -> Int {
21
+ return popAt ( stacks. count - 1 )
22
+ }
23
+
24
+ func popAt( _ index: Int ) -> Int {
25
+ guard index >= 0 , index < stacks. count, !stacks[ index] . isEmpty else {
26
+ return - 1
27
+ }
28
+ let value = stacks [ index] . removeLast ( )
29
+ if stacks [ index] . isEmpty {
30
+ stacks. remove ( at: index)
31
+ }
32
+ return value
33
+ }
34
+ }
35
+
36
+ /**
37
+ * Your StackOfPlates object will be instantiated and called as such:
38
+ * let obj = new StackOfPlates(cap);
39
+ * obj.push(val);
40
+ * let param_2 = obj.pop();
41
+ * let param_3 = obj.popAt(index);
42
+ */
You can’t perform that action at this time.
0 commit comments