-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.go
43 lines (38 loc) · 965 Bytes
/
Solution.go
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
43
type StackOfPlates struct {
stk [][]int
cap int
}
func Constructor(cap int) StackOfPlates {
return StackOfPlates{[][]int{}, cap}
}
func (this *StackOfPlates) Push(val int) {
if this.cap == 0 {
return
}
if len(this.stk) == 0 || len(this.stk[len(this.stk)-1]) >= this.cap {
this.stk = append(this.stk, []int{})
}
this.stk[len(this.stk)-1] = append(this.stk[len(this.stk)-1], val)
}
func (this *StackOfPlates) Pop() int {
return this.PopAt(len(this.stk) - 1)
}
func (this *StackOfPlates) PopAt(index int) int {
ans := -1
if index >= 0 && index < len(this.stk) {
t := this.stk[index]
ans = t[len(t)-1]
this.stk[index] = this.stk[index][:len(t)-1]
if len(this.stk[index]) == 0 {
this.stk = append(this.stk[:index], this.stk[index+1:]...)
}
}
return ans
}
/**
* Your StackOfPlates object will be instantiated and called as such:
* obj := Constructor(cap);
* obj.Push(val);
* param_2 := obj.Pop();
* param_3 := obj.PopAt(index);
*/