-
Notifications
You must be signed in to change notification settings - Fork 5
/
min-stack.go
49 lines (39 loc) · 1 KB
/
min-stack.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
44
45
46
47
48
49
// @Title: 最小栈 (Min Stack)
// @Author: KivenC
// @Date: 2019-07-10 15:21:23
// @Runtime: 32 ms
// @Memory: 7.4 MB
type MinStack struct {
Stack []int
Helper []int
}
/** initialize your data structure here. */
func Constructor() MinStack {
return MinStack{[]int{}, []int{}}
}
func (this *MinStack) Push(x int) {
this.Stack = append(this.Stack, x)
if len(this.Helper) > 0 && this.Helper[len(this.Helper)-1] < x {
this.Helper = append(this.Helper, this.Helper[len(this.Helper)-1])
} else {
this.Helper = append(this.Helper, x)
}
}
func (this *MinStack) Pop() {
this.Helper = this.Helper[:len(this.Helper)-1]
this.Stack = this.Stack[:len(this.Stack)-1]
}
func (this *MinStack) Top() int {
return this.Stack[len(this.Stack)-1]
}
func (this *MinStack) GetMin() int {
return this.Helper[len(this.Helper)-1]
}
/**
* Your MinStack object will be instantiated and called as such:
* obj := Constructor();
* obj.Push(x);
* obj.Pop();
* param_3 := obj.Top();
* param_4 := obj.GetMin();
*/