提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章来源:https://www.toymoban.com/news/detail-795319.html
前言
在数据流中计算平均数
文章来源地址https://www.toymoban.com/news/detail-795319.html
一、力扣295. 数据流的中位数
class MedianFinder {
private PriorityQueue<Integer> small;
private PriorityQueue<Integer> large;
public MedianFinder() {
small = new PriorityQueue<>(
(a,b)->{
return b-a;
}
);
large = new PriorityQueue<>();
}
public void addNum(int num) {
if(small.size() >= large.size()){
small.offer(num);
large.offer(small.poll());
}else{
large.offer(num);
small.offer(large.poll());
}
}
public double findMedian() {
if(small.size() > large.size()){
return small.peek();
}else if(large.size() > small.size()){
return large.peek();
}else{
return (large.peek() + small.peek()) / 2.0;
}
}
}
/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder obj = new MedianFinder();
* obj.addNum(num);
* double param_2 = obj.findMedian();
*/
到了这里,关于力扣labuladong——一刷day93的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!