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 堆的更多相关文章

  1. 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

  2. [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 ...

  3. [LeetCode] 295. Find Median from Data Stream ☆☆☆☆☆(数据流中获取中位数)

    295. Find Median from Data Stream&数据流中的中位数 295. Find Median from Data Stream https://leetcode.co ...

  4. [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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. Find Median from Data Stream

    常规方法 超时 class MedianFinder { vector<int> coll; public: MedianFinder(){ } void heapfu(vector< ...

  9. 数据结构与算法(1)支线任务8——Find Median from Data Stream

    题目如下:(https://leetcode.com/problems/find-median-from-data-stream/) Median is the middle value in an ...

随机推荐

  1. How to build CppCMS 1.x.x

    How to build CppCMS 1.x.x Requirements Mandatory Requirements Recommended Dependencies Suggested Dep ...

  2. Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements

    Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements 开始想写一个 ...

  3. c++之——————各种变量

    对我们程序员来讲,“变量”和“对象”是可以相互互换使用的.-------------开篇之词. 变量:提供一个具有名字的可供程序操作的存储空间.由类型说明符和其后紧跟的数个列表组成,其中变量名之间使用 ...

  4. 0604-面向对象、类与对象、类、static、构造方法/析构方法

    一.面向对象 1.面向过程:一个人分步骤完成某个事情 2.面向对象:某件事情拆分为多个任务,由每个对象独立完成,最后调用整合为一个完整的项目 3.三要素:继承.封装.多态. 封装:私有化属性 提供公共 ...

  5. 此文章介绍vue-cli脚手架config目录下index.js配置文件

    此配置文件是用来定义开发环境和生产环境中所需要的参数 关于注释 当涉及到较复杂的解释我将通过标识的方式(如(1))将解释写到单独的注释模块,请自行查看 上代码 // see http://vuejs- ...

  6. 应用JavaScript搭建一个简易页面图片无缝滚动效果

    页面图片无缝滚动JavaScript原理:移动的区块包含图片内容,区块相对父级元素进行定位脱离文档流.再令区块的left值每隔固定的时间进行等量减少(或增大)从而实现区块的匀速运动.由于每次间隔移动的 ...

  7. go的接口

    一.接口定义 接口类型 在讲基础数据类型时,我们曾提了一下 interface 数据类型,这个数据类型就是接口类型 什么是接口 Go 语言不是"传统"的面向对象的编程语言:它里面没 ...

  8. [Luogu2324]八数码难题

    抱歉...我可能真的做搜索上瘾了... 还是IDA*,自己看看就好了... 注意一下搜索顺序 #include<cstdio> #include<queue> #include ...

  9. Android开机图片替换

    Android开机图片替换 Android从启动到进入Launcher一共会展示三张图片,如果只是更换静态图则更换这三张图片即可,要想换成动画那就要另外操作. 首先查找这个文件:    /bootab ...

  10. 出现“ORA-28000:the account is locked”的解决办法

    在Oracle 11g版本中,出于安全的考虑,所有Oracle的默认用户,包括SCOTT用户都被锁定.输入用户名和口令之后,会出现错误“ORA-28000:the account is locked” ...