LeetCode:155 最小栈

class MinStack {
    Deque<Integer> stack;
    Deque<Integer> min_stack;
    /** initialize your data structure here. */
    public MinStack() {
        stack = new LinkedList<>();
        min_stack = new LinkedList<>();

    }
    
    public void push(int x) {
        stack.push(x);
        if(min_stack.size()!=0)
            min_stack.push(Math.min(min_stack.peek(),x));
        else{
            min_stack.push(x);
        }
    }
    
    public void pop() {
        stack.pop();
        min_stack.pop();
    }
    
    public int top() {
        return stack.peek();
    }
    
    public int getMin() {
        return min_stack.peek();
    }
}

 

posted @ 2020-10-04 08:58  dlooooo  阅读(71)  评论(0编辑  收藏  举报