这道题我们先用heap做了,之后再用排序的方法处理。
public class Solution {
public int findKthLargest(int[] nums, int k) {
PriorityQueue<Integer> queue = new PriorityQueue<>();
for (int i : nums) {
queue.offer(i);
if (queue.size() > k) {
queue.poll();
}
}
return queue.peek();
}
}