题目如下:(https://leetcode.com/problems/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 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.

题目需要实现两个函数:向一个整数表中添加元素以及找中位数。以下是两个思路:

思路一

如果用vector储存整数,找中位数较容易(O(1)),添加整数可能会较耗时间,于是尝试使用时间复杂度为O(log(n))的二分插入。

 class MedianFinder {
public:
bool odd; //长度是否为奇数。奇数为true,偶数为false
int begin, end, mid; //数组头、尾、中间的下标
vector<int> l; //数组 MedianFinder(): odd(false), begin(), end(), mid() {} //添加整数
void addNum(int num) {
//更新
odd = odd ? false : true;
begin = ;
end = l.size() - ;
//二分查找
while (begin <= end)
{
mid = (begin + end) / ;
if (l[mid] == num)
{
break;
}
else if (l[mid] > num)
{
end = mid - ;
}
else
{
begin = mid + ;
}
}
//插入
if (begin > end)
{
l.insert(l.begin()+begin, num);
}
else
{
l.insert(l.begin()+mid, num);
}
} //返回中位数
double findMedian() {
begin = ;
end = l.size() - ;
mid = (begin + end) / ;
if (odd)
{
return l[mid];
}
else
{
return ((double)l[mid] + l[mid+]) / ;
}
}
};

思路二

题目的提示说要用堆,于是考虑stl中的优先队列。可以将中位数两侧的数据分别储存至两个优先队列,一个整数大的优先级高(默认)(相当于排好序的大顶堆),另一个整数小的优先级高(相当于排好序的小顶堆)。

 class MedianFinder {
public:
bool odd; //长度是否为奇数。奇数为true,偶数为false
priority_queue<int> front; //数组的前半部分,优先队列默认为较大的优先级高
priority_queue<int, vector<int>, greater<int>> back; //数组的后半部分,较小的数优先级高 MedianFinder(): odd(false) {} //添加整数
void addNum(int num) {
//更新长度的状态
odd = odd ? false : true;
//插入num,并保证front长度不小于back长度
if (odd)
{
if (back.size() && num > back.top())
{
back.push(num);
front.push(back.top());
back.pop();
}
else
{
front.push(num);
}
}
else
{
if (front.size() && num > front.top())
{
back.push(num);
}
else
{
front.push(num);
back.push(front.top());
front.pop();
}
}
} //返回中位数
double findMedian() {
if (odd)
{
return front.top();
}
else
{
return ((double)front.top() + back.top()) / ;
}
}
};

附:

//先留个坑,考完试再填吧……

数据结构与算法(1)支线任务8——Find Median from Data Stream的更多相关文章

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

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

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

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

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

  4. [Swift]LeetCode295. 数据流的中位数 | 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. 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 ...

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

  8. Find Median from Data Stream 解答

    Question Median is the middle value in an ordered integer list. If the size of the list is even, the ...

  9. LeetCode295-Find Median from Data Stream && 480. 滑动窗口中位数

    中位数是有序列表中间的数.如果列表长度是偶数,中位数则是中间两个数的平均值. 例如, [2,3,4] 的中位数是 3 [2,3] 的中位数是 (2 + 3) / 2 = 2.5 设计一个支持以下两种操 ...

随机推荐

  1. CLR via C# 3rd - 07 - Constants and Fields

    1. Constants        A constant is a symbol that has a never-changing value. When defining a constant ...

  2. Java篇-File类之常用操作

    /** * */ package com.io.file; import java.io.File; import java.io.IOException; /** * <pre> * & ...

  3. This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes.

    -- :::] This application is modifying the autolayout engine from a background thread, which can lead ...

  4. selenium 常见问题整理。

    一:日期控件 selenium不能直接对日期控件操作,可以通过js对日期控件做赋值操作 WebElement inputTimeBox=driver.findElement(by.name(" ...

  5. 各数据库查询前N条记录的SQL语句

    sql在不同数据库查询前几条数据 1. ORACLE SELECT * FROM TABLE_NAME WHERE ROWNUM <= N;  HQL: from table_name t or ...

  6. Visual Studio 创建C++或C#Windows程序

    使用工具Visual Studio,创建一个C++或C#Windows程序,在主窗口中显示“Hello World”,用户可以用鼠标拖动“Hello World”在主窗口中移动 首先创建一个项目​ 选 ...

  7. python之目录文件操作

    [1.os] 1.重命名:os.rename(old, new) 2.删除:os.remove(file) 3.列出目录下的文件 :os.listdir(path) 4.获取当前工作目录:os.get ...

  8. jsp中查询条件的回显

    后台框架为ssh,前台纯手写无框架是最老的写法,因为是接手别人的项目无法改变框架原型,只能基于修改. 进入正题: 我这里查询条件有两种input的text(文本框)和select(下拉框). 1.te ...

  9. etcd第三集

    简单说下golang的etcd接口例子.etcd api有v2(http+json)和v3(grpc)两个版本,目前大家都用v2,所以... v2: https://github.com/coreos ...

  10. php excel读取

    当然首先要判断是否有文件和文件类型,接着把文件保存到某个路径中 /** * 读取excel数据 * @author Red * @date * @param $filename 文件所在路径+文件名 ...