[leetcode] 155. 最小栈
public class MinStack {
Stack<Integer> elements;
Stack<Integer> min;
MinStack() {
elements = new Stack<Integer>();
min = new Stack<Integer>();
}
public int getMin() {
return min.peek();
}
public void push(int x) {
elements.push(x);
if (min.isEmpty() || getMin() > x) {
min.push(x);
} else {
min.push(getMin());
}
}
public int pop() {
min.pop();
return elements.pop();
}
int top() {
return elements.peek();
}
}