1:直观思路,排序后找
public int findKthLargest(int[] nums, int k) {
Arrays.sort(nums);
return nums[nums.length-k];
}
2:利用大根堆nlog(k),亲测没排序快?
public int findKthLargest(int[] nums, int k) {
PriorityQueue<Integer> heap =
new PriorityQueue<>((n1, n2) -> n1 - n2);
for (int n: nums) {
heap.add(n);
if (heap.size() > k){
heap.poll();
}
}
return heap.poll();
}
3:快速排序
4:冒泡排序
5:选择排序
6:计数排序
7:堆排序