Skip to content

Latest commit

 

History

History
37 lines (30 loc) · 688 Bytes

面试题30. 包含min函数的栈.md

File metadata and controls

37 lines (30 loc) · 688 Bytes
type MinStack struct {
	min []int
	nums []int
}

func Constructor() MinStack {
	return MinStack{}
}

func (this *Minstack) Push(x int) {
    this.nums = append(this.nums, x)
    if len(this.min) == 0 {
        this.min = append(this.min, x)
    }
    if this.min[len(this.min)-1] > x {
        this.min = append(this.min, x)
    }else{
        this.min = append(this.min, this.min[len(this.min)-1])
    }
}

func (this *MinStack) Pop(){
    this.nums = this.nums[:len(this.nums)-1]
    this.min = this.min[:len(this.min)-1]
}

func (this *Minstack) Top() int {
    return this.nums[len(this.nums)-1]
}

func (this *Minstack) Min() int {
    return this.min[len(this.min)-1]
}