首页 > 其他 > 详细

LeetCode:155 最小栈

时间:2020-10-04 09:57:46      阅读:34      评论:0      收藏:0      [点我收藏+]
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();
    }
}

 

LeetCode:155 最小栈

原文:https://www.cnblogs.com/dloooooo/p/13766402.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!