码迷,mamicode.com
首页 > 其他好文 > 详细

1-155

时间:2018-09-26 11:41:35      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:this   iat   value   cal   bsp   void   runtime   minstack   top   

class MinStack {
    
    private Stack<Integer> stackData;
    private Stack<Integer> stackMin;

    /** initialize your data structure here. */
    public MinStack() {
        this.stackData=new Stack<Integer>();
        this.stackMin=new Stack<Integer>();
    }
    
    public void push(int x) {
        if(this.stackMin.isEmpty()){
            this.stackMin.push(x);
        }else if(x<=this.getMin()){
            this.stackMin.push(x);
        }
        this.stackData.push(x);
    }
    
    public void pop() {
        if(this.stackData.isEmpty()){
            throw new RuntimeException("Your stack is empty");
        }
        int value=this.stackData.pop();
        if(value==this.getMin()){
            this.stackMin.pop();
        }
    }
    
    public int top() {
        if(this.stackData.isEmpty()){
            throw new RuntimeException("Your stack is empty");
        }
        return this.stackData.peek();
    }
    
    public int getMin() {
        if(this.stackMin.isEmpty()){
            throw new RuntimeException("Your stack is empty");
        }
        return this.stackMin.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();
 */

 

1-155

标签:this   iat   value   cal   bsp   void   runtime   minstack   top   

原文地址:https://www.cnblogs.com/chanaichao/p/9705811.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!