class MinStack {
private Stack<Integer> aStack;
private Stack<Integer> bStack;
public MinStack() {
aStack = new Stack<>();
bStack = new Stack<>();
}
public void push(int x) {
aStack.push(x);
if (bStack.empty() || bStack.peek() >= x) {
bStack.push(x);
}
}
public void pop() {
if (aStack.pop().equals(bStack.peek())) {
bStack.pop();
}
}
public int top() {
return aStack.peek();
}
public int min() {
return bStack.peek();
}
}
image.png
网友评论