forked from awesee/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmin_stack.go
More file actions
39 lines (32 loc) · 762 Bytes
/
min_stack.go
File metadata and controls
39 lines (32 loc) · 762 Bytes
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
package problem155
type MinStack struct {
stack [][2]int
}
/** initialize your data structure here. */
func Constructor() MinStack {
return MinStack{}
}
func (this *MinStack) Push(x int) {
min, l := x, len(this.stack)
if l > 0 && this.stack[l-1][1] < x {
min = this.stack[l-1][1]
}
this.stack = append(this.stack, [2]int{x, min})
}
func (this *MinStack) Pop() {
this.stack = this.stack[:len(this.stack)-1]
}
func (this *MinStack) Top() int {
return this.stack[len(this.stack)-1][0]
}
func (this *MinStack) GetMin() int {
return this.stack[len(this.stack)-1][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();
*/