题目:

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. mockjs 模拟实现增删改查

    /*mUtils.js用于解析get请求的参数*/ export const param2Obj = url => { const search = url.split('?')[1] if ( ...

  2. 【SQL】IN和EXISTS谁的效率更高

    [SQL]IN和EXISTS谁的效率更高 总结: 索引设置好的情况下 子查询数据量大的,用exists 子查询数据量小的,用in 原文连接:https://zhuanlan.zhihu.com/p/4 ...

  3. 力扣21(java&python)-合并两个有序链表(简单)

    题目: 将两个升序链表合并为一个新的 升序 链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例 1: 输入:l1 = [1,2,4], l2 = [1,3,4] 输出:[1,1,2,3 ...

  4. 力扣20(java)-有效的括号(简单)

    题目: 给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合.左括号必须以正确的顺序闭合.每个右括 ...

  5. Serverless Devs 的官网是如何通过 Serverless Devs 部署的

    简介: 只有自己吃自己的狗粮,自己做的东西才不"".Serverless Devs 自发展之处到现在,已经经历了几个月的时间,在这几个月,Serverless Devs 的成长是迅 ...

  6. dotnet 将任意时区的 DateTimeOffset 转换为中国时区时间文本

    本文告诉大家在拿到任意时区的 DateTimeOffset 对象,将 DateTimeOffset 转换为使用中国的 +8 时区表示的时间 在开始之前,需要说明的是,采用 DateTimeOffset ...

  7. Git基础使用指南-命令详解

    Software is like sex: it's better when it's free. -- Linus Torvalds 前情须知 -O- 工作流程 首先要明确的是Git的工作流程,你使 ...

  8. 原生js写悬浮广告效果

    网上抄的,改成vue写法失败,下一篇是自己写的vue版本的 <html> <head> <meta http-equiv="Content-Type" ...

  9. 自动生成robot自动化测试用例

    背景:java项目使用swagger管理接口,随着需求的开发接口也有增加,要从swagger界面中去查找出新增的接口是件很费时,效率很低的事情. 适用情况: java项目且适用swagger管理接口 ...

  10. kali linux 渗透测试 01 kali介绍

    安全问题的根源 分层思想------盲人摸象 只追求功能实现----比较片面 最大的安全威胁是人---- 安全目标 先于攻击者发现和防止漏洞出现 攻击型安全 防护性安全 渗透测试 尝试击破安全防御机制 ...