class MinStack {
Stack<Integer> data = null;
Stack<Integer> helper = null;
/** initialize your data structure here. */
public MinStack() {
data = new Stack<Integer>();
helper = new Stack<Integer>();
}
public void push(int x) {
data.push(x);
if(helper.empty() || (!helper.empty() && x <= helper.peek())){
helper.push(x);
}
}
public void pop() {
int temp = data.pop();
if(temp == helper.peek()){
helper.pop();
}
}
public int top() {
return data.peek();
}
public int getMin() {
return helper.peek();
}
}
155. 最小栈
最新推荐文章于 2022-09-06 18:34:12 发布