Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.
Example 1:
Input: [3,2,1,5,6,4] and k = 2
Output: 5
class Solution {
public int findKthLargest(int[] nums, int k) {
PriorityQueue<Integer> pq = new PriorityQueue<>();//优先级队列
for(int i:nums){
pq.add(i);
if(pq.size()>k){
pq.poll();//删除头指针所指
}
}
return pq.peek();//指向头,不上
}
}