解法I: 对于寻找一个数组中 第n个最大数 或 第n个最小数,可以利用优先队列(大顶堆小顶堆,天然具有排列的性质)
class Solution {
public int findKthLargest(int[] nums, int k) {
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
int length = nums.length, count = 1;
for (int i = 0; i < length; i++) {
priorityQueue.add(nums[i]);
}
while (count++ != k) {
priorityQueue.poll();
}
return priorityQueue.poll();
}
}