Min Stack

作者: carlclone | 来源:发表于2019-07-02 13:28 被阅读0次

复盘:

可以不初始化大小 , 用append可以自动扩容

边界条件判断


LeetCode Min Stack LeetCode Min Stack

这样会减到负值


LeetCode Min Stack LeetCode Min Stack

画图:

LeetCode Min Stack LeetCode Min Stack

pesudo code :

Stack {
    dataArr []int
    dataHead int
}

push (v) {
    dataArr[dataHead]=v
    dataHead++
}

pop () {
    dataHead--
    return dataArr[dataHead]
}

top() {
    return dataArr[datahead-1]
}

getMin() {
    
}



MinStack {
    dataStack Stack
    minStack Stack
}

push (v) {
    dataS.push(v)
    if v>minS.top() {
        minS.push(v)
    }
}

pop () {
    v = dataS.pop()
    if v==minS.top() {
        minS.pop()
    }
    return v
}

top() {
    return dataS.top()
}

getMin() {
    return minS.top()
}

go implementation :

type MinStack struct {
    dataStack *Stack
    minStack *Stack
}


/** initialize your data structure here. */
func Constructor() MinStack {
    return MinStack{dataStack:&Stack{data:make([]int,9999),head:0},minStack:&Stack{data:make([]int,9999),head:0}}
}


func (this *MinStack) Push(x int)  {
    this.dataStack.Push(x)
    if this.minStack.isEmpty() || x<=this.minStack.Top() {
        this.minStack.Push(x)
    }
}


func (this *MinStack) Pop()  {
    v:=this.dataStack.Top()
    if  v==this.minStack.Top() {
        this.minStack.Pop()
    }
    this.dataStack.Pop()
}


func (this *MinStack) Top() int {
    return this.dataStack.Top()
}


func (this *MinStack) GetMin() int {
    return this.minStack.Top()
}


type Stack struct {
    data []int
    head int
}

func (this *Stack) Push(x int)  {

    this.data[this.head]=x
    this.head++
}


func (this *Stack) Pop()  {
    this.head--
}

func (this *Stack) isEmpty() bool {
    if this.head==0 {
        return true

    }
    return false
}


func (this *Stack) Top() int {
    return this.data[this.head-1]
}

相关文章

  • Min Stack

    Easy, Stack Question: 设计一个支持push, pop, top, getMin的堆栈。时间复...

  • Min Stack

    复盘: 可以不初始化大小 , 用append可以自动扩容 边界条件判断 这样会减到负值 画图: pesudo co...

  • Min Stack

    Min Stack Design a stack that supports push, pop, top, an...

  • Leetcode-155Min Stack

    155. Min Stack && 剑指offer-包含min函数的栈 Design a stack that s...

  • Cool Leetcode

    1、Min Stack Design a stack that supports push, pop, top, ...

  • leetcode:155. Min Stack

    155. Min Stack Description Design a stack that supports p...

  • Introduction to Full Stack

    Full stack Introduction to Full Stack15 min preparation A...

  • 155 Min Stack

    Design a stack that supports push, pop, top, and retrievi...

  • 0155 Min Stack

    Design a stack that supports push, pop, top, and retrievi...

  • Lintcode12 Min Stack solution 题解

    【题目描述】 Implement a stack with min() function, which will ...

网友评论

    本文标题:Min Stack

    本文链接:https://www.haomeiwen.com/subject/dvezcctx.html