forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.swift
42 lines (37 loc) · 958 Bytes
/
Solution.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class StackOfPlates {
private var stacks: [[Int]]
private var cap: Int
init(_ cap: Int) {
self.cap = cap
self.stacks = []
}
func push(_ val: Int) {
if cap == 0 {
return
}
if stacks.isEmpty || stacks.last!.count >= cap {
stacks.append([])
}
stacks[stacks.count - 1].append(val)
}
func pop() -> Int {
return popAt(stacks.count - 1)
}
func popAt(_ index: Int) -> Int {
guard index >= 0, index < stacks.count, !stacks[index].isEmpty else {
return -1
}
let value = stacks[index].removeLast()
if stacks[index].isEmpty {
stacks.remove(at: index)
}
return value
}
}
/**
* Your StackOfPlates object will be instantiated and called as such:
* let obj = new StackOfPlates(cap);
* obj.push(val);
* let param_2 = obj.pop();
* let param_3 = obj.popAt(index);
*/