1 Star 0 Fork 1

dllearn/geektime-algorithm-learn

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
f.go 1.06 KB
一键复制 编辑 原始数据 按行查看 历史
dryyun 提交于 2022-04-15 01:11 +08:00 . 0155
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();
*/
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/dllearn/geektime-algorithm-learn.git
git@gitee.com:dllearn/geektime-algorithm-learn.git
dllearn
geektime-algorithm-learn
geektime-algorithm-learn
55fa3f222b36

搜索帮助