AcWing 41. 包含min函数的栈
原题链接
简单
作者:
LangB
,
2020-11-09 13:07:39
,
所有人可见
,
阅读 369
AcWing 41. 包含min函数的栈
class MinStack {
Deque<Integer> stack1;
Deque<Integer> stack2;
/** initialize your data structure here. */
public MinStack() {
stack1 = new ArrayDeque<>();
stack2 = new ArrayDeque<>();
}
public void push(int x) {
stack1.push(x);
int min = stack2.isEmpty() ? x : Math.min(x, stack2.peek());
stack2.push(min);
}
public void pop() {
if (!stack1.isEmpty()) {
stack1.pop();
stack2.pop();
}
}
public int top() {
return stack1.peek();
}
public int getMin() {
return stack2.peek();
}
}