代码拉取完成,页面将自动刷新
package _155_min_stack
// 155. 最小栈
// https://leetcode-cn.com/problems/min-stack/
type MinStack struct {
s []int
preMin []int
}
func Constructor() MinStack {
return MinStack{
s: []int{},
preMin: []int{},
}
}
func (this *MinStack) Push(val int) {
if len(this.s) == 0 {
this.preMin = append(this.preMin, val)
} else {
if this.preMin[len(this.s)-1] > val {
this.preMin = append(this.preMin, val)
} else {
this.preMin = append(this.preMin, this.preMin[len(this.s)-1])
}
}
this.s = append(this.s, val)
}
func (this *MinStack) Pop() {
if len(this.s) == 0 {
return
}
l := len(this.s)
this.s = this.s[:l-1]
this.preMin = this.preMin[:l-1]
}
func (this *MinStack) Top() int {
if len(this.s) == 0 {
return 0
}
return this.s[len(this.s)-1]
}
func (this *MinStack) GetMin() int {
if len(this.s) == 0 {
return 0
}
return this.preMin[len(this.s)-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();
*/
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。