1. 程式人生 > >Min Stack (leetcode 155) go實現

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
}