美文网首页
leetcode:155. Min Stack

leetcode:155. Min Stack

作者: 唐僧取经 | 来源:发表于2018-09-20 09:24 被阅读0次

155. Min Stack

Description

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

push(x) -- Push element x onto stack.
pop() -- Removes the element on top of the stack.
top() -- Get the top element.
getMin() -- Retrieve the minimum element in the stack.
Example:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> Returns -3.
minStack.pop();
minStack.top(); --> Returns 0.
minStack.getMin(); --> Returns -2.

Answer

package main

type MinStack struct {
    stack []int
    min   *node
}

type node struct {
    prevMin *node
    idx     int
}

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

func (m *MinStack) Push(x int) {
    m.stack = append(m.stack, x)
    if m.min == nil || m.stack[m.min.idx] > x {
        n := &node{prevMin: m.min, idx: len(m.stack) - 1}
        m.min = n
    }
}

func (m *MinStack) Pop() {
    idx := len(m.stack) - 1
    if m.min != nil && m.min.idx == idx {
        m.min = m.min.prevMin
    }
    m.stack = m.stack[:idx]
}

func (m *MinStack) Top() int {
    return m.stack[len(m.stack)-1]
}

func (m *MinStack) GetMin() int {
    return m.stack[m.min.idx]
}


相关文章

网友评论

      本文标题:leetcode:155. Min Stack

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