数据流的中位数
Posted Alice_yufeng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据流的中位数相关的知识,希望对你有一定的参考价值。
class MedianFinder
TreeMap<Integer, Integer> nums;
int n;
int[] left;
int[] right;
public MedianFinder()
nums = new TreeMap<Integer, Integer>();
n = 0;
left = new int[2];
right = new int[2];
public void addNum(int num)
nums.put(num, nums.getOrDefault(num, 0) + 1);
if (n == 0)
left[0] = right[0] = num;
left[1] = right[1] = 1;
else if ((n & 1) != 0)
if (num < left[0])
decrease(left);
else
increase(right);
else
if (num > left[0] && num < right[0])
increase(left);
decrease(right);
else if (num >= right[0])
increase(left);
else
decrease(right);
System.arraycopy(right, 0, left, 0, 2);
n++;
public double findMedian()
return (left[0] + right[0]) / 2.0;
private void increase(int[] iterator)
iterator[1]++;
if (iterator[1] > nums.get(iterator[0]))
iterator[0] = nums.ceilingKey(iterator[0] + 1);
iterator[1] = 1;
private void decrease(int[] iterator)
iterator[1]--;
if (iterator[1] == 0)
iterator[0] = nums.floorKey(iterator[0] - 1);
iterator[1] = nums.get(iterator[0]);
/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder obj = new MedianFinder();
* obj.addNum(num);
* double param_2 = obj.findMedian();
*/
以上是关于数据流的中位数的主要内容,如果未能解决你的问题,请参考以下文章