30. 包含min函数的栈
借助辅助栈
代码:
class MinStack {
LinkedList<Integer> A,B;
/** initialize your data structure here. */
public MinStack() {
A = new LinkedList<>();
B = new LinkedList<>();
}
public void push(int x) {
A.push(x);
if(B.isEmpty() || (B.getFirst() >= x)){
B.push(x);
}
}
public void pop() {
if(A.getFirst().equals(B.getFirst())){
B.pop();
}
A.pop();
}
public int top() {
return A.getFirst();
}
public int min() {
return B.getFirst();
}
}
/**
* 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();
*/