题目
题源
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6bb31a374b2d0b5f81776fb80097d85d.png)
代码
class MinStack {
private Deque<Integer> stack;
private Deque<Integer> miniStack;
public MinStack() {
stack = new LinkedList<Integer>();
miniStack = new LinkedList<Integer>();
}
public void push(int val) {
stack.offerFirst(val);
if (miniStack.isEmpty() || miniStack.peekFirst() >= val) {
miniStack.offerFirst(val);
}
}
public void pop() {
int spop = stack.pollFirst();
if (miniStack.peekFirst() == spop) {
miniStack.pollFirst();
}
}
public int top() {
return stack.peekFirst();
}
public int getMin() {
if (miniStack.isEmpty()) {
}
return miniStack.peekFirst();
}
}