leetcode - 待优化!!!

class MinStack {

    private Stack<Integer> stk ;
    private Stack<Integer> minStk;
    /** initialize your data structure here. */
    public MinStack() {
        stk = new Stack<>();
        minStk =new Stack<>();
    }
    
    public void push(int x) {
        stk.push(x);
        if(minStk.isEmpty()){
            minStk.push(x);
        }else{
            int min = minStk.peek();
            minStk.push(Math.min(min,x));
        }
    }
    
    public void pop() {
       int e= stk.pop();
       minStk.pop();
    }
    
    public int top() {
        return stk.peek();
    }
    
    public int getMin() {
        return minStk.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();
 */

  

猜你喜欢

转载自www.cnblogs.com/leodaxin/p/11300641.html