zoukankan      html  css  js  c++  java
  • 剑指offer30-包含min函数的栈

    class MinStack {
    
        /** initialize your data structure here. */
        Stack<Integer> A, B;
        public MinStack() {
            A = new Stack<>();
            B = new Stack<>();
        }
        //这里要注意始终保持B栈顶最小
        public void push(int x) {
            A.add(x);
            if(B.isEmpty()||x<=B.peek()){
                B.add(x);
            }
        }
        //pop时候要注意A和B的一致性
        public void pop() {
            if(A.pop().equals(B.peek())){
                B.pop();
            }
            
        }
        
        public int top() {
            return A.peek();
        }
        
        public int min() {
            return B.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.min();
     */
  • 相关阅读:
    awk使用
    SQL VIEW(视图)
    crontab使用
    SecureCRT
    Python异常
    Python字符串
    Python字典,列表,元组
    Python路径
    vim插件
    Python类
  • 原文地址:https://www.cnblogs.com/jieyi/p/14267150.html
Copyright © 2011-2022 走看看