题目描述
如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值。
解题思路
需要两个数据结构,能够对进入数据排序,并且两数据结构大小小于等于 1。中位数只在数据交接点出现。
采用 PriorityQueue 实现大顶堆和小顶堆。
Java 默认是小顶堆,重新定义 Comparator 实现大顶堆。
同时满足以下条件:
-
两个堆中的数据数目差不能超过 1,这样可以使中位数只会出现在两个堆的交接处;
-
大顶堆的所有数据都小于小顶堆,这样就满足了排序要求。
代码
import java.util.Comparator; import java.util.PriorityQueue; public class Solution { int count; PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>(); PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(11, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { //PriorityQueue默认是小顶堆,实现大顶堆,需要反转默认排序器 return o2.compareTo(o1); } }); public void Insert(Integer num) { count++; if ((count & 1) == 0) { if (!maxHeap.isEmpty() && num < maxHeap.peek()) { maxHeap.offer(num); num = maxHeap.poll(); } minHeap.offer(num); } else { if (!minHeap.isEmpty() && num > minHeap.peek()) { minHeap.offer(num); num = minHeap.poll(); } maxHeap.offer(num); } } public Double GetMedian() { if (count == 0) return 0.0; if ((count & 1) == 1) return (double)maxHeap.peek(); else return (double)(minHeap.peek() + maxHeap.peek()) / 2.0; } }
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于