用优先队列
public PriorityQueue<Integer> kthLargest(int k, int[]a) { PriorityQueue<Integer> q = new PriorityQueue<>(k); for (int i : a) { if (q.size() < k) { q.offer(i); }else { if (i > q.peek()) { q.poll(); q.offer(i); } } } return q; }
2024-03-16 19:56:52
用优先队列
public PriorityQueue<Integer> kthLargest(int k, int[]a) { PriorityQueue<Integer> q = new PriorityQueue<>(k); for (int i : a) { if (q.size() < k) { q.offer(i); }else { if (i > q.peek()) { q.poll(); q.offer(i); } } } return q; }