题目:
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.
For example,
Given [3,2,1,5,6,4]
and k = 2, return 5.
public class Solution {
public int findKthLargest(int[] nums, int k) {
PriorityQueue< Integer> heap = new PriorityQueue<Integer>(k,new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
// TODO Auto-generated method stub
return o2-o1;
}
});
for(int i=0;i<nums.length;i++)
{
heap.offer(nums[i]);
}
for(int i=1;i<k;i++)
{
heap.poll();
}
return heap.poll();
}
}