最小栈

class MinStack{
        private Stack<Integer> dataStack = new Stack<Integer>();
        private Stack<Integer> minStack = new Stack<Integer>();
        
        public void push(int x){
            dataStack.add(x);
            if(minStack.isEmpty() || minStack.peek()>=x){
                minStack.add(x);
            }else{
                minStack.add(minStack.peek());
            }
        }
        
        public void pop(){
            dataStack.pop();
            minStack.pop();
        }
        
        public int peek(){
            return dataStack.peek();
        }
        
        public int getMin(){
            return minStack.peek();
        }
    }

 

posted @ 2020-08-06 14:53  helloworldmybokeyuan  阅读(51)  评论(0编辑  收藏  举报