LeetCodeAnimation/0215-Kth-Largest-Element-in-an-Array/Code/1.java

18 lines
524 B
Java
Raw Normal View History

2020-04-19 09:09:30 +08:00
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();
}
}