最小栈

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();
        }
    }

 

上一篇:0. 定义整型但输入字符串


下一篇:javascript数据结构——栈