剑指 Offer II 059. 数据流的第 K 大数值
作者:互联网
class KthLargest {
int k;
PriorityQueue<Integer> pq;
public KthLargest(int k, int[] nums) {
this.k = k;
pq = new PriorityQueue<>();
for (int i = 0; i < nums.length; i++) {
add(nums[i]);
}
}
public int add(int val) {
if (pq.size() < k){
pq.add(val);
}
else {
if (val > pq.peek()){
pq.poll();
pq.add(val);
}
}
return pq.peek();
}
}
https://leetcode-cn.com/problems/jBjn9C/
标签:KthLargest,pq,val,Offer,int,nums,II,add,059 来源: https://www.cnblogs.com/taoyuann/p/15496220.html