动态数组,效率不高。
class MinStack {
private ArrayList <Integer> list = new ArrayList <Integer> ();
private int MIN;
public MinStack() {
}
public void push(int val) {
list.add(val);
}
public void pop() {
list.remove(list.size() - 1);
}
public int top() {
return list.get(list.size() - 1);
}
public int getMin() {
MIN = Integer.MAX_VALUE;
for (int i = 0; i < list.size(); i++) {
if (list.get(i) < MIN) {
MIN = list.get(i);
}
}
return MIN;
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(val);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/