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 {
public: // Adds a number into the data structure.
void addNum(int num) {
maxHeap.push(num);
int tmp = maxHeap.top();
maxHeap.pop();
minHeap.push(tmp);
if(minHeap.size() > maxHeap.size()){
tmp = minHeap.top();
minHeap.pop();
maxHeap.push(tmp);
}
} // Returns the median of current data stream
double findMedian() {
int m = maxHeap.size();
int n = minHeap.size();
if(m > n)
return maxHeap.top()/1.0;
else
return (maxHeap.top() + minHeap.top())/2.0;
}
private:
priority_queue<int, vector<int>, greater<int>> maxHeap;
priority_queue<int, vector<int>, less<int>> minHeap;
};

LeetCode OJ:Find Median from Data Stream(找数据流的中数)的更多相关文章

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

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

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

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

  4. leetcode@ [295]Find Median from Data Stream

    https://leetcode.com/problems/find-median-from-data-stream/ Median is the middle value in an ordered ...

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

  6. LeetCode——295. Find Median from Data Stream

    一.题目链接: https://leetcode.com/problems/find-median-from-data-stream 二.题目大意: 给定一段数据流,要求求出数据流中的中位数,其中数据 ...

  7. [LeetCode] 346. Moving Average from Data Stream 从数据流中移动平均值

    Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...

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

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

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

随机推荐

  1. 对称加密与非对称加密,以及RSA的原理

    一 , 概述 在现代密码学诞生以前,就已经有很多的加密方法了.例如,最古老的斯巴达加密棒,广泛应用于公元前7世纪的古希腊.16世纪意大利数学家卡尔达诺发明的栅格密码,基于单表代换的凯撒密码.猪圈密码, ...

  2. Django学习笔记之Django ORM相关操作

    一般操作 详细请参考官方文档 必知必会13条 <> all(): 查询所有结果 <> filter(**kwargs): 它包含了与所给筛选条件相匹配的对象 <> ...

  3. Django框架搭建(windows系统)

    Django框架搭建(windows系统) 一.Django简介 开放源代码的Web应用框架,由Python语言编写,一个大而全的框架. 1.web框架介绍 具体介绍Django之前,必须先介绍WEB ...

  4. mouseover 有一个多次触发的问题

    mouseover 有一个多次触发的问题 需要注意 由于浏览器的冒泡行为.造成如果在一个DIV元素上同时定义了mouseover,mouseout的时候,当鼠标移动到DIV中的child子元素的时候, ...

  5. 20145301《Java程序设计》实验报告一:Java开发环境的熟悉

    20145301<Java程序设计>实验报告一:Java开发环境的熟悉 课程:Java程序设计 实验名称:Java开发环境的熟悉 实验目的与要求: 1.没有Linux基础的同学建议先学习& ...

  6. 20145302张薇《Java程序设计》实验四报告

    20145325张薇 实验四:Andoid开发基础 实验内容 使用 Android Studio 设计"Hello" 设计过程 首先创建项目 选择.xml中的`Design 选中W ...

  7. 20145314郑凯杰 《Java程序设计》实验二 实验报告

    20145314郑凯杰 <Java程序设计>实验二 实验报告 实验要求 完成实验.撰写实验报告,实验报告以博客方式发表在博客园,注意实验报告重点是运行结果,遇到的问题(工具查找,安装,使用 ...

  8. 20145329 《JAVA程序设计》实验三总结

    实验日期:2016.4.12 实验时间:15:30~17:30 实验序号:实验三 实验名称: 敏捷开发与XP实践 实验目的与要求: XP基础 XP核心实践 相关工具 实验内容 1.使用git托管代码 ...

  9. 20144303 《Java程序设计》第八周学习总结

    20144303 <Java程序设计>第八周学习总结 教材学习内容总结 第十五章 1.日志API简介: java.util.logging包提供了日志功能相关类与接口,不必额外配置日志组件 ...

  10. linux共享内存简单介绍以及编码演示

    共享内存的基本概念 共享内存区是最快的IPC形式.一旦这样的内存映射到共享它的进程的地址空间,这些进程间数据传递不再涉及到内核,换句话说是进程不再通过执行进入内核的系统调用来传递彼此的数据. 下图是共 ...