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]
  2. [1, 1], [3, 3]
  3. [1, 1], [3, 3], [7, 7]
  4. [1, 3], [7, 7]
  5. [1, 3], [6, 7]


  1. #include<iostream>
  2. #include<vector>
  3. #include<algorithm>
  4. #include<cstdio>
  5.  
  6. using namespace std;
  7.  
  8. struct Interval {
  9. int start;
  10. int end;
  11. Interval() : start(), end() {}
  12. Interval(int s, int e) : start(s), end(e) {}
  13. };
  14. bool Cmp(Interval a, Interval b) { return a.start < b.start; }//不能将其放在class SummaryRange中
  15. class SummaryRanges {
  16. public:
  17. void addNum(int val) {
  18.  
  19. vector<Interval>::iterator it = lower_bound(vec.begin(), vec.end(), Interval(val, val), Cmp);
  20. int start = val, end = val;
  21. if(it != vec.begin() && (it-)->end+ >= val) it--;
  22. while(it != vec.end() && val+ >= it->start && val- <= it->end)
  23. {
  24. start = min(start, it->start);
  25. end = max(end, it->end);
  26. it = vec.erase(it);
  27. }
  28. vec.insert(it,Interval(start, end));
  29. }
  30.  
  31. vector<Interval> getIntervals() {
  32. return vec;
  33. }
  34. private:
  35. vector<Interval> vec;
  36. };
  37.  
  38. /**
  39. * Your SummaryRanges object will be instantiated and called as such:
  40. * SummaryRanges* obj = new SummaryRanges();
  41. * obj->addNum(val);
  42. * vector<Interval> param_2 = obj->getIntervals();
  43. */
  44.  
  45. int main(){
  46. SummaryRanges* obj =new SummaryRanges();
  47. obj->addNum();
  48. obj->addNum();
  49. obj->addNum();
  50. obj->addNum();
  51. obj->addNum();
  52. vector<Interval> param_2 = obj->getIntervals();
  53. cout<<param_2[].start<<"------"<<param_2[].end<<endl;
  54. cout<<param_2[].start<<"------"<<param_2[].end<<endl;
  55. }


易错点:

  1. vector<Interval>::iterator it = lower_bound(vec.begin(), vec.end(), Interval(val, val), Cmp);
    lower_bound()返回的是从beginend之间的按照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

  1.  

352[LeetCode] Data Stream as Disjoint Intervals的更多相关文章

  1. [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流

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

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

  3. [LeetCode] 352. Data Stream as Disjoint Intervals 分离区间的数据流

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

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

  5. 【leetcode】352. Data Stream as Disjoint Intervals

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

  6. 352. Data Stream as Disjoint Intervals

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

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

  8. [Swift]LeetCode352. 将数据流变为多个不相交间隔 | Data Stream as Disjoint Intervals

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

  9. [leetcode]352. Data Stream as Disjoint Intervals

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

随机推荐

  1. Maven--archetypeCatalog笔记

    当我们使用maven原型生成项目骨架时,经常会在[INFO] Generating project in Interactive mode这个地方特别慢,这里并不是什么出错卡住的原因,你打开mvn的d ...

  2. DW CS5序列号

    先要改host文件,以防止其连接 Adobe 的激活验证服务器 1. 用记事本编辑“C:\Windows\System32\Drivers\etc\”目录下的 hosts 文件, 在末尾加上: 127 ...

  3. Plugin 'InnoDB' registration as a STORAGE ENGINE failed

    今天在安装mysql时遇到了mysql服务打不开的的情况,通过在cmd中输入MySQL --console,显示错误信息,得到如下情况. 原因是InnoDB初始化异常,也就是是说,卸载mysql的时候 ...

  4. Python实现音乐的剪辑

    一.读取音频文件 from scipy.io import wavfile import numpy as np like = wavfile.read('./嘤嘤嘤.wav') print (lik ...

  5. 记一次PHP实现接收邮件信息(我这里测试的腾讯企业邮件)

    PHP实现接收邮件信息(我这里测试的腾讯企业邮件) , 其他的类型的没有测,应该只要更换pop3地址 端口号就可以. 代码如下(代码参考网络分享): <?php //此处查看链接状态 heade ...

  6. NUCLEO-L053R8 TIM定时器 PWM输出

    TIM2 PWM输出测试 今天给大伙分享一个TIM2 PWM输出小实验. 实验开发板:Nucleo-L053R8,即STM32L053R8T6. 开发环境:MDK5 图1 - 工程界面 本次实验测试的 ...

  7. MDK/Keil 中,J-Link调试查看变量值总是显示<not in scope>

    转载请注明出处,谢谢. MDK/Keil 中,J-Link调试查看变量值总是显示<not in scope> 原因:编译器把代码优化掉了,直接导致在仿真中变量根本没有分配内存,也就无法查看 ...

  8. python 的 购物小程序

    money = input('请输入您的工资:') shop = [("iphone",5800),("ipod",3000),("book" ...

  9. vue-router核心概念

    vue用来实现SPA的插件 使用vue-router 1. 创建路由器: router/index.js new VueRouter({ routes: [ { // 一般路由 path: '/abo ...

  10. 成都优步uber司机第三组奖励政策

    今天成都优步又推出了优步司机第三组,第一二组的奖励大家都晓得,但是第三组的奖励怎么样呢?还是先看看官方给出的消息. 滴滴快车单单2.5倍,注册地址:http://www.udache.com/如何注册 ...