• 155 最小栈


    import java.util.Stack;
    
    class MinStack {
    
        //没有必要把所有元素都记录下来
        Stack<Integer> stack = new Stack<>();
        Stack<Integer> minStack = new Stack<>();
    
        /** initialize your data structure here. */
        public MinStack() {
    
        }
    
        public void push(int x) {
            stack.push(x);
            if(!minStack.isEmpty()){
                if(x <= minStack.peek())
                    minStack.push(x);
            }
            else{
                minStack.push(x);
            }
        }
    
        public void pop() {
            if(!stack.isEmpty()){
                int temp = stack.pop();
                if(!minStack.isEmpty()){
                    if(temp == minStack.peek()){
                        minStack.pop();
                    }
                }
            }
        }
    
        public int top() {
                return stack.peek();
        }
    
        public int getMin() {
                return minStack.peek();
        }
    }
    
    /**
     * Your MinStack object will be instantiated and called as such:
     * MinStack obj = new MinStack();
     * obj.push(x);
     * obj.pop();
     * int param_3 = obj.top();
     * int param_4 = obj.getMin();
     */
    
  • 相关阅读:
    离线存储
    创业公司 加入
    console 代理
    HTTP 协议中 Vary 的一些研究
    reactNative 的一些学习
    srcset 图片自适应
    一些不错的文章分享
    前端网站大全
    c# 捕捉键盘按键
    SQL 中With as 的用法
  • 原文地址:https://www.cnblogs.com/realzhaijiayu/p/13548184.html
Copyright © 2020-2023  润新知