295. Find Median from Data Strea
作者:
夜皇雪 | 来源:发表于
2016-12-12 12:32 被阅读0次public class MedianFinder {
private Queue<Long> small = new PriorityQueue(),
large = new PriorityQueue();
public void addNum(int num) {
large.add((long) num);
small.add(-large.poll());
if (large.size() < small.size())
large.add(-small.poll());
}
public double findMedian() {
return large.size() > small.size()
? large.peek()
: (large.peek() - small.peek()) / 2.0;
}
};
// Your MedianFinder object will be instantiated and called as such:
// MedianFinder mf = new MedianFinder();
// mf.addNum(1);
// mf.findMedian();
本文标题:295. Find Median from Data Strea
本文链接:https://www.haomeiwen.com/subject/xmemmttx.html
网友评论