min_stack
min_stack.go 源码
package main
import (
"math"
)
//最小栈
type MinStack struct {
stack []int
min []int
}
func Constructor() MinStack {
return MinStack{[]int{}, []int{math.MaxInt64}}
}
func (this *MinStack) Push(x int) {
this.stack = append(this.stack, x)
this.min = append(this.min, min(x, this.min[len(this.min)-1]))
}
func (this *MinStack) Pop() {
this.stack = this.stack[:len(this.stack)-1]
this.min = this.min[:len(this.min)-1]
}
func (this *MinStack) Top() int {
return this.stack[len(this.stack)-1]
}
func (this *MinStack) GetMin() int {
return this.min[len(this.min)-1]
}
func min(x int, y int) int {
if x < y {
return x
}
return y
}
你可能感兴趣的文章
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦