051_最小栈

知识点:栈

LeetCode第一百五十五题:https://leetcode-cn.com/problems/min-stack/submissions/

语言:GoLang

type MinStack struct {
    stack       [][]int
}


/** initialize your data structure here. */
func Constructor() MinStack {
    return MinStack{
        [][]int{},
    }
}


func (this *MinStack) Push(x int)  {
    min := this.GetMin()
    value := []int{x}

    if min > x {
        value = append(value, x)
    }else {
        value = append(value, min)
    }
    this.stack = append(this.stack, value)
}


func (this *MinStack) Pop()  {
    size := len(this.stack)
    if size == 0 {
        return
    }

    this.stack = this.stack[:size - 1]
}


func (this *MinStack) Top() int {
    size := len(this.stack)
    if size == 0 {
        return 0
    }

    return this.stack[size - 1][0]
}


func (this *MinStack) GetMin() int {
    size := len(this.stack)
    if size == 0 {
        return math.MaxInt64
    }

    return this.stack[size - 1][1]
}


/**
 * 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();
 */
posted @ 2020-04-09 21:07  Cenyol  阅读(93)  评论(0)    收藏  举报