Min Stack (leetcode 155) go實現

type MinStack struct {
    Val int
    Min int
    Next *MinStack
}


/** initialize your data structure here. */
func Constructor() MinStack {
    return MinStack{0, 0, nil}
}


func (this *MinStack) Push(x int)  {
    Min := x
    if this.Next != nil {
        Min = int(math.Min(float64(x), float64(this.Next.Min)))
    }
    temp := &MinStack{Val:x, Min:Min, Next:this.Next}
    this.Next = temp
}


func (this *MinStack) Pop()  {
    if this.Next == nil {
        return
    }
    this.Next = this.Next.Next
}


func (this *MinStack) Top() int {
    if this.Next == nil {
        return 0
    }
    return this.Next.Val
}


func (this *MinStack) GetMin() int {
    if this.Next == nil {
        return 0
    }
    return this.Next.Min
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章