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种,插入头部,尾部,中间,插入头部,不管插入哪里,都判断一下左边和右边是否能和当前的数字接起来,我这样 ...
随机推荐
- H5 移动端开发中 ios/安卓坑 和经验总结
1. ios new时间对象,需要用逗号隔开传日期的方式, 不支持 new Date('2019-03-01 08:00:00') 格式: 支持以下两种方式: 2. ios个别版本对fixed的属性的 ...
- windows安装的mysql中文乱码的坑
本机装的mysql为5.6的,从代码执行的中文inert语句总是显示问号,然后在中文查询是都会报问题 今天终于解决了! 问题解决方法为: 找到my.ini文件在文件中加入 [client]defaul ...
- NLP语言模型
语言模型: I. 基本思想 区别于其他大多数检索模型从查询到文档(即给定用户查询,如何找出相关的文档), 语言模型由文档到查询,即为每个文档建立不同的语言模型,判断由文档生成用户查 询的可能性有多大, ...
- RMAN_PIPE
涉及的dbms_pipe包中的过程和函数:(1)PACK_MESSAGE Procedures用途:Builds message in local buffer(2)SEND_MESSAGE Func ...
- ABAP术语-Transaction
Transaction 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/19/1112804.html Logical process in ...
- SpringBoot整合Mybatis,TypeAliases配置失败的问题
SpringBoot整合Mybatis,TypeAliases配置失败的问题 问题描述 在应用MyBatis时,使用对象关系映射,将对象和Aliase映射起来. 在Mybatis的文档明确写出,如果你 ...
- 小白CSS学习日记-----杂乱无序记录(3)
1.后代选择器 .antzone li { } class='antzone' 所有子孙后代中的li 2.子选择器 .antzone > li { } class='antzone' 的子一 ...
- php ecshop 二级域名切换跳转时session不同步,解决session无法共享同步导致无法登陆或者无法退出的问题
echshop基础上做了单点登录的 一级域名与二级域名 退出时 清空session 都是一级域名的session 因为二级域名的session是设置在二级域名上的 echshop基础上没有做单点登录的 ...
- day3-exercise
# Author: 刘佳赐-Isabelle October 28,2018 """ 1. 文件a1.txt内容 序号 部门 人数 平均年龄 备注 1 python 30 ...
- 第4天 Java基础语法
第4天 Java基础语法 今日内容介绍 流程控制语句(switch) 数组 流程控制语句 选择结构switch switch 条件语句也是一种很常用的选择语句,它和if条件语句不同,它只能针对某个表达 ...