mirror of
https://gitee.com/TheAlgorithms/LeetCodeAnimation.git
synced 2024-12-06 15:19:44 +08:00
18 lines
524 B
Java
18 lines
524 B
Java
class Solution {
|
|
public int findKthLargest(int[] nums, int k) {
|
|
// // 创建一个小顶堆(优先队列模拟)
|
|
PriorityQueue<Integer> heap =
|
|
new PriorityQueue<Integer>();
|
|
|
|
// 在堆中维护当前最大k个元素
|
|
for (int i = 0; i < nums.length; i++){
|
|
if(heap.size() < k){
|
|
heap.add(nums[i]);
|
|
}else if (heap.element() < nums[i]){
|
|
heap.poll();
|
|
heap.add(nums[i]);
|
|
}
|
|
}
|
|
return heap.poll();
|
|
}
|
|
} |