-
Notifications
You must be signed in to change notification settings - Fork 0
/
155.go
47 lines (40 loc) · 922 Bytes
/
155.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
package p155
type MinStack struct {
stack []int
min []int
length int
}
/** initialize your data structure here. */
func Constructor() MinStack {
return MinStack{stack: make([]int, 0), min: make([]int, 0), length: 0}
}
func (this *MinStack) Push(x int) {
this.stack = append(this.stack, x)
if this.length > 0 && this.min[this.length-1] < x {
x = this.min[this.length-1]
}
this.min = append(this.min, x)
this.length++
}
func (this *MinStack) Pop() {
if this.length == 0 {
return
}
this.stack = this.stack[:this.length-1]
this.min = this.min[:this.length-1]
this.length--
}
func (this *MinStack) Top() int {
return this.stack[this.length-1]
}
func (this *MinStack) GetMin() int {
return this.min[this.length-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();
*/