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) {
|
|||
|
// // <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>С<EFBFBD><D0A1><EFBFBD>ѣ<EFBFBD><D1A3><EFBFBD><EFBFBD>ȶ<EFBFBD><C8B6><EFBFBD>ģ<EFBFBD>⣩
|
|||
|
PriorityQueue<Integer> heap =
|
|||
|
new PriorityQueue<Integer>();
|
|||
|
|
|||
|
// <20>ڶ<EFBFBD><DAB6><EFBFBD>ά<EFBFBD><CEAC><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>k<EFBFBD><6B>Ԫ<EFBFBD><D4AA>
|
|||
|
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();
|
|||
|
}
|
|||
|
}
|