[LeetCode] Find Median from Data Stream
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.
Examples:
[2,3,4]
, the median is 3
[2,3]
, the median is (2 + 3) / 2 = 2.5
Design a data structure that supports the following two operations:
- void addNum(int num) - Add a integer number from the data stream to the data structure.
- double findMedian() - Return the median of all elements so far.
For example:
- add(1)
- add(2)
- findMedian() -> 1.5
- add(3)
- findMedian() -> 2
- class MedianFinder {
- private:
- multiset<int> left, right;
- public:
- // Adds a number into the data structure.
- void addNum(int num) {
- if (right.empty() || num < *right.begin()) left.insert(num);
- else right.insert(num);
- }
- // Returns the median of current data stream
- double findMedian() {
- int size = left.size() + right.size();
- while (left.size() > size / ) {
- int tmp = *left.rbegin();
- right.insert(tmp);
- left.erase(left.find(tmp));
- }
- while (left.size() < size / ) {
- int tmp = *right.begin();
- left.insert(tmp);
- right.erase(right.find(tmp));
- }
- if (size & ) return *right.begin();
- else return (double)(*left.rbegin() + *right.begin()) / ;
- }
- };
- // Your MedianFinder object will be instantiated and called as such:
- // MedianFinder mf;
- // mf.addNum(1);
- // mf.findMedian();
[LeetCode] Find Median from Data Stream的更多相关文章
- [LeetCode] Find Median from Data Stream 找出数据流的中位数
Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...
- LeetCode——Find Median from Data Stream
Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...
- [LeetCode] 295. Find Median from Data Stream ☆☆☆☆☆(数据流中获取中位数)
295. Find Median from Data Stream&数据流中的中位数 295. Find Median from Data Stream https://leetcode.co ...
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- leetcode@ [295]Find Median from Data Stream
https://leetcode.com/problems/find-median-from-data-stream/ Median is the middle value in an ordered ...
- [LeetCode] 295. Find Median from Data Stream 找出数据流的中位数
Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...
- 【LeetCode】295. Find Median from Data Stream 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆+小根堆 日期 题目地址:https://le ...
- [leetcode]295. Find Median from Data Stream数据流的中位数
Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...
- LeetCode OJ:Find Median from Data Stream(找数据流的中数)
Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...
随机推荐
- matlab微分方程dsolve使用
y=dsolve('Dy=exp(-x-y-2)','y(0)=-2','x') dy/dx 写成Dy (注意大小写) y(0)=-2 表示初始条件 'x'表示积分变量
- my sql
如果改了上面的4个配置文件,要让其立即生效,可以使用如下方法 source .bash_profile . .bash_profile 基于Apache+php+mysql的许愿墙网站的搭建 方案一: ...
- STL(1)
这一篇因为游戏设计而写的,里面采用了STL,先借用一下,过段时间专项研究. 模板 模板就是一种通用化的类,同一种模板可以创建无数种具有共同特征的容器类型.首先需要指定基础类型,比如int ,char, ...
- Rsync 3.1.0 发布,文件同步工具
文件同步工具Rsync 3.1.0发布.2013-09-29 上一个版本还是2011-09-23的3.0.9 过了2年多.Rsync基本是Linux上文件同步的标准了,也可以和inotify配合做实时 ...
- Android 5.x特性概览五
上节,介绍Material Design 对阴影效果的实现,这节,我们来介绍Android 5.x的着色与裁剪的特性. Android 5.X 在对图像的操作上增加更多的功能,下面来看看 Androi ...
- c#中的static
1.C# 不支持静态局部变量(在方法范围内声明的变量). 2.static类一般用于与状态无关的类.那么,什么是与状态无关的类?我的理解是当一个类中没有属性,只有方法的的时候,就可以认为这个类是与状态 ...
- C#设计模式(8)——桥接模式(Bridge Pattern)
一.引言 这里以电视遥控器的一个例子来引出桥接模式解决的问题,首先,我们每个牌子的电视机都有一个遥控器,此时我们能想到的一个设计是——把遥控器做为一个抽象类,抽象类中提供遥控器的所有实现,其他具体电视 ...
- Hadoop HDFS 架构设计
HDFS 简介 Hadoop Distributed File System,简称HDFS,是一个分布式文件系统. HDFS是高容错性的,可以部署在低成本的硬件之上,HDFS提供高吞吐量地对应用程序数 ...
- 第十八章:Android 打包部署
Andriod应用程序如果要在手机或模拟器上安装,必须要有签名! 1.签名的意义 为了保证每个应用程序开发商合法ID,防止部分开放商可能通过使用相同的Package Name来混淆替换已经安装的程序, ...
- [ucgui] 对话框3——GUIBuilder生成界面c文件及修改
>_<" 其实生成GUI有相应的工具: