352[LeetCode] Data Stream as Disjoint Intervals

时间:2022-09-07 08:39:23

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的更多相关文章

  1. &lbrack;LeetCode&rsqb; Data Stream as Disjoint Intervals 分离区间的数据流

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  2. Leetcode&colon; Data Stream as Disjoint Intervals &amp&semi;&amp&semi; Summary of TreeMap

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  3. &lbrack;LeetCode&rsqb; 352&period; Data Stream as Disjoint Intervals 分离区间的数据流

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  4. leetcode&commat; &lbrack;352&rsqb; Data Stream as Disjoint Intervals &lpar;Binary Search &amp&semi; TreeSet&rpar;

    https://leetcode.com/problems/data-stream-as-disjoint-intervals/ Given a data stream input of non-ne ...

  5. 【leetcode】352&period; Data Stream as Disjoint Intervals

    问题描述: Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers ...

  6. 352&period; Data Stream as Disjoint Intervals

    Plz take my miserable life T T. 和57 insert interval一样的,只不过insert好多. 可以直接用57的做法一个一个加,然后如果数据大的话,要用tree ...

  7. 352&period; Data Stream as Disjoint Intervals &lpar;TreeMap&comma; lambda&comma; heapq&rpar;

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  8. &lbrack;Swift&rsqb;LeetCode352&period; 将数据流变为多个不相交间隔 &vert; Data Stream as Disjoint Intervals

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  9. &lbrack;leetcode&rsqb;352&period; Data Stream as Disjoint Intervals

    数据流合并成区间,每次新来一个数,表示成一个区间,然后在已经保存的区间中进行二分查找,最后结果有3种,插入头部,尾部,中间,插入头部,不管插入哪里,都判断一下左边和右边是否能和当前的数字接起来,我这样 ...

随机推荐

  1. JavaScript数组:增删改查、排序等

    直接上代码 // 数组应用 var peoples = ["Jack","Tom","William","Tod",&q ...

  2. block、inline、inline-block

    block: block - 块级元素 常见的块级元素包括:div,form,p,table,ul,ol,dl,h1~h6,pre block 可以包含 inlne 和 block 和 inline- ...

  3. 分享到QQ空间代码&lpar;一&rpar;

    如何给自己的网站添上"分享到QQ空间"的功能? 只要选择以下代码嵌入自己的网页,即可将网站的信息分享到QQ空间

  4. oc-22-sel

    /** sel: 1.作用:包装方法 2.格式:typedef struct objc_selector *SEL; 3.用法: SEL 名称 = @selector(方法); 调用形式: [对象 p ...

  5. asp&period;net中的mysql传参数MySqlParameter

    注意在asp.net中传参 string sql="select name,id from user where id=@id"; //@idm不需要引号 MySqlParamet ...

  6. Google AppEngine 创建的例子

    1.guestbook: http://chenyy-gac-test.appspot.com/ 2.time clock: http://chenyy-gac-20150922.appspot.co ...

  7. L - Subway - POJ 2502

    题意:在一个城市里,分布着若干条地铁线路,每条地铁线路有若干个站点,所有地铁的速度均为40km/h.现在你知道了出发地和终点的坐标,以及这些地铁 线路每个站点的坐标,你的步行速度为10km/h,且你到 ...

  8. 一步步教你读懂NET中IL(附带图)

    一步步教你读懂NET中IL(附带图) 接触NET也有1年左右的时间了,NET的内部实现对我产生了很大的吸引力,在msdn上找到一篇关于NET的IL代码的图解说明,写的挺不错的.个人觉得:能对这些底部的 ...

  9. python if&comma;for&comma;while

    # -*- coding:utf-8 -*- # 第四章 if for while #布尔逻辑 print True == False print True and False print True ...

  10. sleep、yield、wait、join的区别&lpar;阿里面试&rpar;

    1.  Thread.sleep(long) 和Thread.yield()都是Thread类的静态方法,在调用的时候都是Thread.sleep(long)/Thread.yield()的方式进行调 ...