其他分享
首页 > 其他分享> > 最小栈

最小栈

作者:互联网

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

 

标签:peek,minStack,int,最小,public,dataStack,Stack
来源: https://www.cnblogs.com/helloworldmybokeyuan/p/13446339.html