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. Java 接口(interface)的三种类型

    放入接口中的任何域(成员变量)都自动是 static 和 final 的: 1. 包含抽象方法的常规接口 2. 全部是常量的 接口类中的方法和属性不要添加任何修饰符号(public 也不需要). 因为 ...

  2. Coursera Algorithms Programming Assignment 2: Deque and Randomized Queue (100分)

    作业原文:http://coursera.cs.princeton.edu/algs4/assignments/queues.html 这次作业与第一周作业相比,稍微简单一些.有三个编程练习:双端队列 ...

  3. scrollTop,scrollHeight,clientTop,clientHeight,offsetTop,offsetHeight实际意义 及 计算方式 附实例说明

    一.滚动距离.高度 scrollTop scrollLeft scrollHeight scrollWidth 二.相对位置.距离 offsetTop offsetLeft offsetHeight ...

  4. Cracking the Coding Interview 8.7

    Given a infinite number of quarters(25cents), dimens(10cents), nickels(5cents) and pennies(1cent), w ...

  5. sqlserver 树结构递归(向上递归和向下递归)

    --获取当前及以下部门 Create proc GetCurrentAndUnderOrg @orgId int as begin WITH cte AS ( SELECT * ,0 AS level ...

  6. 能够附加图片的标签控件iOS项目源码

    这个源码案例是能够附加图片的标签控件,源码JTImageLabel,JTImageLabel能够附加图片的标签Label控件,图片可以随意更换.位置也能够很好的控制.效果图: <ignore_j ...

  7. OpenCV:OpenCV目标检测Hog+SWindow源代码分析

    参考文章:OpenCV中的HOG+SVM物体分类 此文主要描述出HOG分类的调用堆栈. 使用OpenCV作图像检测, 使用HOG检测过程,其中一部分源代码如下: 1.HOG 检测底层栈的检测计算代码: ...

  8. 【sqli-labs】 less10 GET - Blind - Time based. - Double quotes (基于时间的双引号盲注)

    这个和less9一样,单引号改完双引号就行了 http://localhost/sqli/Less-10/?id=1" and sleep(5)%23 5s后页面完成刷新 http://lo ...

  9. swift 类型系统 Self self Type

    namedClass:静态类型:与类型实现直接关联:可以用于初始化.类型检查等. namedClass.self:@thick,脱敏(脱关)类型:动态类型:可以作为元类型的实例:可以作为类型参量进行传 ...

  10. monad - the Category hierachy

    reading the "The Typeclassopedia" by Brent Yorgey in Monad.Reader#13 ,and found that " ...