AcWing 41. 包含min函数的栈
原题链接
简单
作者:
小轩喵灬
,
2025-01-13 23:41:21
,
所有人可见
,
阅读 1
class MinStack {
/** initialize your data structure here. */
private Deque<Integer> deque;
private Deque<Integer> minDeque;
public MinStack() {
deque = new ArrayDeque<>();
minDeque = new ArrayDeque<>();
}
public void push(int x) {
deque.push(x);
if (minDeque.isEmpty() || minDeque.peek() >= x) {
minDeque.push(x);
}
}
public void pop() {
if (!minDeque.isEmpty() && minDeque.peek().equals(deque.peek())) {
minDeque.pop();
}
deque.pop();
}
public int top() {
return deque.peek();
}
public int getMin() {
return minDeque.peek();
}
}