【发布时间】:2020-07-23 13:00:53
【问题描述】:
这是我的代码。
class MinStack {
public Deque<Integer> deque = new LinkedList<Integer>();
public PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
public MinStack() {
Deque<Integer> deque = new LinkedList<Integer>();
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
}
public void push(int x) {
deque.offer(x);
pq.offer(x);
}
public void pop() {
pq.remove(deque.peek());
deque.pollLast();
}
public int top() {
return deque.peekLast();
}
public int getMin() {
return pq.peek();
}
}
在函数 pop() 中,PriorityQueue 不会删除我从 deque.peek() 获得的最高值。 当我将其更改为
pq.remove(deque.pollLast());
成功了。这是为什么呢?
【问题讨论】: