find-median-from-data-stream & multiset priority queue 堆
https://leetcode.com/problems/find-median-from-data-stream/
这道题目实在是不错,所以单独拎出来。
https://discuss.leetcode.com/topic/27521/short-simple-java-c-python-o-log-n-o-1/2
看这里面的C++解法,用了priority_queue来实现的。(其实也是堆,之前我一直用multiset)
而Java用的PriorityQueue来做。
关于multiset 和 priority queue的区别,可以看这里:
https://discuss.leetcode.com/topic/27521/short-simple-java-c-python-o-log-n-o-1/2
priority_queue 调用 STL里面的 make_heap(), pop_heap(), push_heap() 算法实现。
注意,priority queue默认是大顶堆,top()和pop()都是针对最大的元素。
而Java的PriorityQueue是从小到大排的。
注意下面对于比较方法的初始化(尤其是比较方式参数的传入)
// constructing priority queues
#include <iostream> // std::cout
#include <queue> // std::priority_queue
#include <vector> // std::vector
#include <functional> // std::greater class mycomparison
{
bool reverse;
public:
mycomparison(const bool& revparam=false)
{reverse=revparam;}
bool operator() (const int& lhs, const int&rhs) const
{
if (reverse) return (lhs>rhs);
else return (lhs<rhs);
}
}; int main ()
{
int myints[]= {,,,}; std::priority_queue<int> first;
std::priority_queue<int> second (myints,myints+);
std::priority_queue<int, std::vector<int>, std::greater<int> >
third (myints,myints+);
// using mycomparison:
typedef std::priority_queue<int,std::vector<int>,mycomparison> mypq_type; mypq_type fourth; // less-than comparison
mypq_type fifth (mycomparison(true)); // greater-than comparison return ;
}
find-median-from-data-stream & multiset priority queue 堆的更多相关文章
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- [LeetCode] Find Median from Data Stream
Find Median from Data Stream Median is the middle value in an ordered integer list. If the size of t ...
- [LeetCode] 295. Find Median from Data Stream ☆☆☆☆☆(数据流中获取中位数)
295. Find Median from Data Stream&数据流中的中位数 295. Find Median from Data Stream https://leetcode.co ...
- [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 ...
- 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 ...
- leetcode笔记:Find Median from Data Stream
一. 题目描写叙述 Median is the middle value in an ordered integer list. If the size of the list is even, th ...
- Find Median from Data Stream
常规方法 超时 class MedianFinder { vector<int> coll; public: MedianFinder(){ } void heapfu(vector< ...
- 数据结构与算法(1)支线任务8——Find Median from Data Stream
题目如下:(https://leetcode.com/problems/find-median-from-data-stream/) Median is the middle value in an ...
随机推荐
- openStack 主机流量计运行状态 随笔记录
root@ruiy-controller:~# ifconfigeth0 Link encap:Ethernet HWaddr 0c:c4:7a:0d:97:2c ine ...
- 75.培训管理-培训信息发布 Extjs 页面
1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8&quo ...
- sublime的ctags安装
首先,是ctags的下载.在这里:http://pan.baidu.com/s/1gdAMFab 我们用sublime几乎都会首先安装这个插件,这个插件是管理插件的功能,先安装它,再安装其他插件就方便 ...
- hdu 3037Saving Beans(卢卡斯定理)
Saving Beans Saving Beans Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- sql 添加列并设置默认值
ALTER TABLE tablsename ADD fieldname BIT NULL DEFAULT
- windows 装XP系统
笔记本型号:HPCQ40-506AX 1.在BIOS中更改启动顺序:将USB设为第一启动项2.插入装有PE系统的USB设备3.开机后一直按F124.到达选择系统界面,目前我的HPCQ40用其他系统进去 ...
- 黑客常用dos命令
http://blog.csdn.net/CSDN___LYY/article/details/77802438
- HDU_5724_状态压缩的sg函数
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5724 题目大意:n行20列的棋盘,对于每行,如果当前棋子右边没棋子,那可以直接放到右边,如果有就跳过放 ...
- jstl与el结合常见用法
JSTL Functions标签库 在JSP文件中使用Functions标签库,要先通过taglib指令引入该标签库: <%@taglib uri=”http://java.sun.com/js ...
- JavaEE的起步
因为某些原因,现在要从.net开发转向J2EE了,在这里记录一下学习经历