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
}

 

    原文作者:算法
    原文地址: https://www.twblogs.net/a/5bd3afbf2b717778ac20b255
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注