题目:

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.

For example,

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

Example:

addNum(1)
addNum(2)
findMedian() -> 1.5
addNum(3)
findMedian() -> 2

分析:

中位数是有序列表中间的数。如果列表长度是偶数,中位数则是中间两个数的平均值。

很明显我们最好不要每调用一次求中位数函数就重新计算一遍,这样做时间复杂度较高。

剑指offer中有一道相同的题目,可以参考这篇理解这道题,剑指Offer-63.数据流中的中位数(C++/Java)

程序:

C++

class MedianFinder {
public:
/** initialize your data structure here. */
MedianFinder() {
index = 0;
} void addNum(int num) {
if(index % 2 == 0){
minHeap.push(num);
maxHeap.push(minHeap.top());
minHeap.pop();
}
else{
maxHeap.push(num);
minHeap.push(maxHeap.top());
maxHeap.pop();
}
index++;
} double findMedian() {
double res = 0;
if(index % 2 == 0){
res = (double)(maxHeap.top() + minHeap.top()) / 2;
return res;
}
else{
res = (double)maxHeap.top();
return res;
}
}
private:
priority_queue <int, vector<int>, less<int> > maxHeap;
priority_queue <int, vector<int>, greater<int> > minHeap;
int index;
};

Java

class MedianFinder {

    /** initialize your data structure here. */
public MedianFinder() {
minHeap = new PriorityQueue<Integer>();
maxHeap = new PriorityQueue<Integer>(11,new Comparator<Integer>(){
@Override
public int compare(Integer i1,Integer i2){
return i2-i1;
}
});
index = 0;
} public void addNum(int num) {
if(index % 2 == 0){
minHeap.offer(num);
maxHeap.offer(minHeap.poll());
}
else{
maxHeap.offer(num);
minHeap.offer(maxHeap.poll());
}
index++;
} public double findMedian() {
double res = 0;
if(index % 2 == 0){
res = (minHeap.peek() + maxHeap.peek()) / 2.0;
return res;
}
else{
res = maxHeap.peek();
return res;
}
}
private PriorityQueue<Integer> minHeap;
private PriorityQueue<Integer> maxHeap;
private int index;
}

LeetCode 295. Find Median from Data Stream数据流的中位数 (C++/Java)的更多相关文章

  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. 295 Find Median from Data Stream 数据流的中位数

    中位数是排序后列表的中间值.如果列表的大小是偶数,则没有中间值,此时中位数是中间两个数的平均值.示例:[2,3,4] , 中位数是 3[2,3], 中位数是 (2 + 3) / 2 = 2.5设计一个 ...

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

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

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

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

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

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

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

  8. 【LeetCode】295. Find Median from Data Stream 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 大根堆+小根堆 日期 题目地址:https://le ...

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

  10. [LC] 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 ...

随机推荐

  1. 【Oracle】在PL/SQL中使用sql实现选择排序

    [Oracle]在PL/SQL中使用sql实现选择排序 一般来说,SQL要排序的话直接使用order by即可 不一般来说,就是瞎搞,正好也可以巩固自己的数据结构基础 使用SQL实现排序系列: 使用S ...

  2. 【Oracle】year must be between -4713 and +9999,and not be 0

    [Oracle]year must be between -4713 and +9999,and not be 0 year must be between -4713 and +9999,and n ...

  3. 力扣378(java&python)-有序矩阵中第 K 小的元素(中等)

    题目: 给你一个 n x n 矩阵 matrix ,其中每行和每列元素均按升序排序,找到矩阵中第 k 小的元素.请注意,它是 排序后 的第 k 小元素,而不是第 k 个 不同 的元素. 你必须找到一个 ...

  4. HarmonyOS NEXT应用开发—城市选择案例

    介绍 本示例介绍城市选择场景的使用:通过AlphabetIndexer实现首字母快速定位城市的索引条导航. 效果图预览 使用说明 分两个功能 在搜索框中可以根据城市拼音模糊搜索出相近的城市,例如输入& ...

  5. Quick BI产品核心功能大图(六)开放集成

    ​简介:企业想要拥有领先的数据分析能力,自研往往需要投入巨大的人力和财力. Quick BI作为唯一一个连续两年入选Gartner魔力象限的中国BI产品,具备强大的全链路开放集成能力,可以轻松的与企业 ...

  6. MongoDB 5.0新特性概览

    ​简介: MongoDB 5.0标志着一个新的发布周期的到来,以更快地交付新特性给到用户.版本化API与在线重新分片相结合,使用户不必担心未来的数据库升级以及业务变化问题:本地原生时间序列数据平台也使 ...

  7. WPF 触摸底层 PenImc 是如何工作的

    在 WPF 里面有其他软件完全比不上的超快速的触摸,这个触摸是通过 PenImc 获取的.现在 WPF 开源了,本文就带大家来阅读触摸底层的代码,阅读本文需要一点 C# 和 C++ 基础 现在 WPF ...

  8. Vue2源码解析-源码调试与核心流程梳理图解

    现在VUE3已经有一段时间了,也慢慢普及起来了.不过因为一直还在使用VUE2的原因还是去了解和学了下它的源码,毕竟VUE2也不会突然就没了是吧,且VUE3中很多原理之类的也是类似的.然后就准备把VUE ...

  9. Java Collection接口下的“ List 集合” 与 “ Set 集合 ”

    Java Collection接口下的" List 集合" 与 " Set 集合 " 每博一文案 一个人最好的底牌,就这两个字: 靠谱,是最高级的聪明. 师父说 ...

  10. 配置vscode的vue环境变量

    参考下面连接: vscode中使用beautify插件格式化vue文件(自定义快捷键) https://blog.csdn.net/jiandan1127/article/details/859589 ...