-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path155.最小栈.go
56 lines (47 loc) · 1.04 KB
/
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
48
49
50
51
52
53
54
55
/*
* @lc app=leetcode.cn id=155 lang=golang
*
* [155] 最小栈
*/
// @lc code=start
func min(x, y int) int {
if x < y {
return x
} else {
return y
}
}
type MinStack struct {
stack []int
minStack []int
}
func Constructor() MinStack {
return MinStack{[]int{}, []int{}}
}
func (this *MinStack) Push(val int) {
if len(this.minStack) == 0 || this.minStack[len(this.minStack)-1] >= val {
this.minStack = append(this.minStack, val)
}
this.stack = append(this.stack, val)
}
func (this *MinStack) Pop() {
if this.stack[len(this.stack)-1] == this.minStack[len(this.minStack)-1] {
this.minStack = this.minStack[0 : len(this.minStack)-1]
}
this.stack = this.stack[0 : len(this.stack)-1]
}
func (this *MinStack) Top() int {
return this.stack[len(this.stack)-1]
}
func (this *MinStack) GetMin() int {
return this.minStack[len(this.minStack)-1]
}
/**
* Your MinStack object will be instantiated and called as such:
* obj := Constructor();
* obj.Push(val);
* obj.Pop();
* param_3 := obj.Top();
* param_4 := obj.GetMin();
*/
// @lc code=end