class MinStack { /** initialize your data structure here. */ Stack<Integer> A, B; public MinStack() { A = new Stack<>(); B = new Stack<>(); } //这里要注意始终保持B栈顶最小 public void push(int x) { A.add(x); if(B.isEmpty()||x<=B.peek()){ B.add(x); } } //pop时候要注意A和B的一致性 public void pop() { if(A.pop().equals(B.peek())){ B.pop(); } } public int top() { return A.peek(); } public int min() { return B.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.min(); */