/** initialize your data structure here. */ func Constructor() MinStack { return MinStack{Val: []int{}, Min: 0} }
func (this *MinStack) Push(x int) { if x < this.GetMin() || len(this.Val) == 0 { this.Min = x } this.Val = append(this.Val, x) }
func (this *MinStack) Pop() { top := this.Top() this.Val = this.Val[:len(this.Val)-1] if top > this.Min { return } this.Min = this.Top() for _, v := range this.Val { if v < this.Min { this.Min = v } } }
func (this *MinStack) Top() int { if len(this.Val) > 0 { returnthis.Val[len(this.Val)-1] } return0 }
func (this *MinStack) GetMin() int { returnthis.Min }
/** * Your MinStack object will be instantiated and called as such: * obj := Constructor(); * obj.Push(x); * obj.Pop(); * param_3 := obj.Top(); * param_4 := obj.GetMin(); */