352[LeetCode] Data Stream as Disjoint Intervals
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen so far as a list of disjoint intervals.
For example, suppose the integers from the data stream are 1, 3, 7, 2, 6, ..., then the summary will be:
[1, 1]
[1, 1], [3, 3]
[1, 1], [3, 3], [7, 7]
[1, 3], [7, 7]
[1, 3], [6, 7]
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio> using namespace std; struct Interval {
int start;
int end;
Interval() : start(), end() {}
Interval(int s, int e) : start(s), end(e) {}
};
bool Cmp(Interval a, Interval b) { return a.start < b.start; }//不能将其放在class SummaryRange中
class SummaryRanges {
public:
void addNum(int val) { vector<Interval>::iterator it = lower_bound(vec.begin(), vec.end(), Interval(val, val), Cmp);
int start = val, end = val;
if(it != vec.begin() && (it-)->end+ >= val) it--;
while(it != vec.end() && val+ >= it->start && val- <= it->end)
{
start = min(start, it->start);
end = max(end, it->end);
it = vec.erase(it);
}
vec.insert(it,Interval(start, end));
} vector<Interval> getIntervals() {
return vec;
}
private:
vector<Interval> vec;
}; /**
* Your SummaryRanges object will be instantiated and called as such:
* SummaryRanges* obj = new SummaryRanges();
* obj->addNum(val);
* vector<Interval> param_2 = obj->getIntervals();
*/ int main(){
SummaryRanges* obj =new SummaryRanges();
obj->addNum();
obj->addNum();
obj->addNum();
obj->addNum();
obj->addNum();
vector<Interval> param_2 = obj->getIntervals();
cout<<param_2[].start<<"------"<<param_2[].end<<endl;
cout<<param_2[].start<<"------"<<param_2[].end<<endl;
}
易错点:
vector<Interval>::iterator it = lower_bound(vec.begin(), vec.end(), Interval(val, val), Cmp);
lower_bound()返回的是从begin到end之间的按照Cmp排序的首个大于等于Interval(val,val)的地址;
因此it 声明应该是 vector<Interval>::iterator it
之前错误的声明为 Interval* it 则出现报错:[Error] cannot convert '__gnu_cxx::__normal_iterator<Interval*, std::vector<Interval> >' to 'Interval*' in initialization
352[LeetCode] Data Stream as Disjoint Intervals的更多相关文章
- [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
- Leetcode: Data Stream as Disjoint Intervals && Summary of TreeMap
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
- [LeetCode] 352. Data Stream as Disjoint Intervals 分离区间的数据流
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
- leetcode@ [352] Data Stream as Disjoint Intervals (Binary Search & TreeSet)
https://leetcode.com/problems/data-stream-as-disjoint-intervals/ Given a data stream input of non-ne ...
- 【leetcode】352. Data Stream as Disjoint Intervals
问题描述: Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers ...
- 352. Data Stream as Disjoint Intervals
Plz take my miserable life T T. 和57 insert interval一样的,只不过insert好多. 可以直接用57的做法一个一个加,然后如果数据大的话,要用tree ...
- 352. Data Stream as Disjoint Intervals (TreeMap, lambda, heapq)
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
- [Swift]LeetCode352. 将数据流变为多个不相交间隔 | Data Stream as Disjoint Intervals
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
- [leetcode]352. Data Stream as Disjoint Intervals
数据流合并成区间,每次新来一个数,表示成一个区间,然后在已经保存的区间中进行二分查找,最后结果有3种,插入头部,尾部,中间,插入头部,不管插入哪里,都判断一下左边和右边是否能和当前的数字接起来,我这样 ...
随机推荐
- css百分比问题——`top`、`left`、'translate'的百分比参照谁?
比如 top.left.transform属性的translate方法,他们的百分比都是相较谁而言的? top.left是基于父元素的: .parent { position: relative; b ...
- servlet,过滤器,监听器,拦截器的区别
一.目录 1.概念 2.生命周期 3.职责 4.执行过程 二.内容 概念 1.servlet:servlet是一种运行服务器端的java应用程序,具有独立于平台和协议的特性, 可以动态生成web页面它 ...
- Swift_下标
Swift_下标 点击查看源码 func testSubscripts() { //下标关键字subscript //array测试 struct TestArray { //内部数组 var arr ...
- C++常用的系统函数
数学<math.h>: 1 三角函数 double sin (double); double cos (double); double tan (double); 2 反三角函数 doub ...
- ORACLE GOLDEN GATE oracle同步数据至kafka
一.服务器信息 ip 软件版本 ogg版本 软件包 操作系统版本 OGG安装路径 10.1.50.52 源 oracle11.2.0.4 12.2.0.1.1 V100692-01.zip cen ...
- 简单json---转树形json
var data = [ {"fileName":"navone","layFilterId":"layadmin-system- ...
- idea配置SpringBoot热部署之自动Build
一.pom.xml文件导入所需依赖文件 SpringBoot热部署插件 <dependency> <groupId>org.springframework.boot</g ...
- 内网环境下为Elasticsearch 5.0.2 添加head服务
背景: 本项目的服务器是内网环境,没有网络,因此需要在离线的环境中,安装head服务. 需要用到的安装包有: node的安装包 elasticsearch的head插件源码 说明:此次只讲述为elas ...
- MFC非模态添加进程控件方法一(线程方法)
由于非模态对话框的自己没有消息循环,创建后无法进行消息处理.需要和父窗口共用消息循环.如果单独在子窗口进行控件由于自己没有单独的消息循环,更新是无法进行的. 如果在父窗口更新控件会造成程序假死.如以下 ...
- MFC实现http连接、发送和接收数据
#include <afxinet.h> // 设置超时 CInternetSession session; session.SetOption(INTERNET_OPTION_CONNE ...