真是误人子弟,leetcode155题官网的golang答案是错误的。
push方法的append操作,必然不能保证是o(1)的时间复杂度。
就这还是官网的标准答案,就这水平,😄
leetcode误人子弟不是第一次了。光会刷算法,可惜水平还是这么次,代码写出来全部是错误的。

附上我的代码:
type MinStack struct {
MinValue [30000]int
InnerStack [30000]int
topIndex int
}
func Constructor() MinStack {
minStack := MinStack{topIndex: -1, MinValue: [30000]int{}, InnerStack: [30000]int{}}
return minStack
}
func (this *MinStack) Push(val int) {
this.InnerStack[this.topIndex+1] = val
if this.topIndex == -1 || this.MinValue[this.topIndex] > val {
this.MinValue[this.topIndex+1] = val
} else {
this.MinValue[this.topIndex+1] = this.MinValue[this.topIndex]
}
this.topIndex++
}
func (this *MinStack) Pop() {
this.topIndex--
}
func (this *MinStack) Top() int {
return this.InnerStack[this.topIndex]
}
func (this *MinStack) GetMin() int {
return this.MinValue[this.topIndex]
}

被折叠的 条评论
为什么被折叠?



