typeMinStackstruct{stack[]intminint}/** initialize your data structure here. */funcConstructor()MinStack{returnMinStack{}}func(this*MinStack)Push(xint){iflen(this.stack)==0{this.stack=append(this.stack,0)this.min=x}else{this.stack=append(this.stack,x-this.min)ifthis.min>x{this.min=x}}}func(this*MinStack)Pop(){n:=len(this.stack)-1ifn<0{panic("no value to popup!")}ifthis.stack[n]<0{this.min=this.min-this.stack[n]}this.stack=this.stack[:n]}func(this*MinStack)Top()int{n:=len(this.stack)-1// 注意:不能直接返回 this.stack[n] + this.min
ifthis.stack[n]>0{returnthis.stack[n]+this.min}else{returnthis.min}}func(this*MinStack)Min()int{returnthis.min}