class MinStack {
//定义一个数据栈s1,一个最小元素栈minStack
Stack<Integer> s1,minStack;
/** initialize your data structure here. */
public MinStack() {
s1 = new Stack<>();
minStack = new Stack<>();
}
public void push(int x) {
s1.push(x);
if(minStack.isEmpty() || x <= minStack.peek()){
minStack.push(x);
}
}
public void pop() {
//不能用== ,用equals
if(minStack.peek().equals(s1.peek())){
minStack.pop();
}
s1.pop();
}
public int top() {
return s1.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();
*/