本文主要是介绍力扣labuladong——一刷day93,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
- 前言
- 一、力扣295. 数据流的中位数
前言
在数据流中计算平均数
一、力扣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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!