美文网首页
155. 最小栈

155. 最小栈

作者: Andysys | 来源:发表于2019-12-30 22:24 被阅读0次
        class MinStack {
            private Stack<Integer> stack;
            private Stack<Integer> minStack;
    
            /** initialize your data structure here. */
            public MinStack() {
                stack = new Stack<>();
                minStack = new Stack<>();
            }
    
            public void push(int x) {
                stack.push(x);
                if (minStack.isEmpty() || x <= minStack.peek()) {
                    minStack.push(x);
                }
            }
    
            public void pop() {
                if (stack.pop().equals(minStack.peek())) {
                    minStack.pop();
                }
            }
    
            public int top() {
                return stack.peek();
            }
    
            public int getMin() {
                return minStack.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();
     */
    

    相关文章

      网友评论

          本文标题:155. 最小栈

          本文链接:https://www.haomeiwen.com/subject/vbbeoctx.html