数据流中的中位数
来源:互联网 发布:python 搭建技术博客 编辑:程序博客网 时间:2024/06/10 03:15
从一个数据流中获取其中位数
/** * 维护两个优先队列,一个存储从中间到最大值的一半的数top , 这个队列为最小优先队列 * 另一个存储从中间到最小的一半数down, 这个队列为最大优先队列 */import java.util.Comparator;import java.util.PriorityQueue;import java.util.Queue;public class Solution { private Queue<Integer> top = new PriorityQueue<Integer>(11 , new Comparator<Integer>() {@Overridepublic int compare(Integer o1, Integer o2) {if(o1 > o2 )return 1 ; else if(o1 < o2) return -1 ; else return 0; }}) ; private Queue<Integer> down = new PriorityQueue<Integer>(11 , new Comparator<Integer>(){@Overridepublic int compare(Integer o1, Integer o2) {if(o1 > o2)return -1 ; else if(o1 < o2)return 1 ; else return 0;}}) ; public void Insert(Integer num) { if(top.size() == down.size()){ if((!down.isEmpty() && num >= down.peek())||down.isEmpty()){ top.add(num) ; }else{ top.add(down.poll()) ; down.add(num) ; } }else{ if(num <= top.peek()){ down.add(num) ; }else{ down.add(top.poll()) ; top.add(num) ; } } } public Double GetMedian() { if(top.size() > down.size()) return (double)top.peek() ; return (top.peek() + down.peek())/2.0 ; }}
0 0
- 数据流中的中位数
- 数据流中的中位数
- 求数据流中的中位数
- 数据流中的中位数
- 数据流中的中位数
- 数据流中的中位数
- 数据流中的中位数
- 数据流中的中位数
- 数据流中的中位数
- 数据流中的中位数
- 【63】数据流中的中位数
- 数据流中的中位数
- 数据流中的中位数
- Q64:数据流中的中位数
- 数据流中的中位数
- 数据流中的中位数
- 数据流中的中位数
- 数据流中的中位数
- FastDFS介绍和配置过程
- Jquery的 each的使用 $.each()
- getChildDrawingOrder()?
- 让父进程在退出时所有的子进程连同用system启动的进程一同退出
- unity实现dialog效果教程
- 数据流中的中位数
- 修改数据库大小
- Masonry和FDTemplateLayoutCell 结合使用Demo
- 通过VMware建立hadoop集群
- 实验一 实现单处理机下的进程调度程序
- Android之Activity销毁
- R语言学习三
- 巧用&符号,判断一个数的奇偶性,判断一个数(x)是否是2的n次方
- linux arch目录下处理器体系结构介绍