LeetCode "Find Median from Data Stream"
2015-10-20 14:05
351 查看
Similar one as LintCode "Data Stream Median".
class MedianFinder { priority_queue<int> hl; // max-heap priority_queue<int, vector<int>, std::greater<int> > hr; // min-heap public: // Adds a number into the data structure. void addNum(int num) { if(hl.empty()) { hl.push(num); } else { if(num <= hl.top()) { hl.push(num); } else { hr.push(num); } // main if(hl.size() > (hr.size() + 1)) { hr.push(hl.top()); hl.pop(); } else if(hr.size() > hl.size()) { hl.push(hr.top()); hr.pop(); } } } // Returns the median of current data stream double findMedian() { if(hl.size() > hr.size()) { return hl.top() * 1.; } return (hl.top() + hr.top()) / 2.0; } };
相关文章推荐
- CSS背景靠右对齐,并且背景图片右边刘10px
- 【MySQL】【leetcode】 Rising Temperature解题报告
- [svn] linux命令——svn分支创建、合并
- C语言面向对象之继承、多态、可变参数、函数指针
- 从头认识java-1.8 创建和操纵对象(1)
- iOS auto
- sql往主键自增的表插入数据
- Tomcat 7 中的 JDBC Realm 配置
- Liunx sed
- Xcode7 collectionView的编辑新特性
- 从头认识java-1.8 创建和操纵对象(1)
- Qt学习之路_13(简易俄罗斯方块)
- URI和URL的区别
- Python中T-SNE实现降维
- MySql Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts' 解决方法
- 查看 Ubuntu 的发行版信息
- C#带ref和out的反射使用
- update数组实现
- jQuery 获取屏幕高度、宽度
- 8月读书分享-《执行力是训练出来的》