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种,插入头部,尾部,中间,插入头部,不管插入哪里,都判断一下左边和右边是否能和当前的数字接起来,我这样 ...
随机推荐
- Maven--archetypeCatalog笔记
当我们使用maven原型生成项目骨架时,经常会在[INFO] Generating project in Interactive mode这个地方特别慢,这里并不是什么出错卡住的原因,你打开mvn的d ...
- DW CS5序列号
先要改host文件,以防止其连接 Adobe 的激活验证服务器 1. 用记事本编辑“C:\Windows\System32\Drivers\etc\”目录下的 hosts 文件, 在末尾加上: 127 ...
- Plugin 'InnoDB' registration as a STORAGE ENGINE failed
今天在安装mysql时遇到了mysql服务打不开的的情况,通过在cmd中输入MySQL --console,显示错误信息,得到如下情况. 原因是InnoDB初始化异常,也就是是说,卸载mysql的时候 ...
- Python实现音乐的剪辑
一.读取音频文件 from scipy.io import wavfile import numpy as np like = wavfile.read('./嘤嘤嘤.wav') print (lik ...
- 记一次PHP实现接收邮件信息(我这里测试的腾讯企业邮件)
PHP实现接收邮件信息(我这里测试的腾讯企业邮件) , 其他的类型的没有测,应该只要更换pop3地址 端口号就可以. 代码如下(代码参考网络分享): <?php //此处查看链接状态 heade ...
- NUCLEO-L053R8 TIM定时器 PWM输出
TIM2 PWM输出测试 今天给大伙分享一个TIM2 PWM输出小实验. 实验开发板:Nucleo-L053R8,即STM32L053R8T6. 开发环境:MDK5 图1 - 工程界面 本次实验测试的 ...
- MDK/Keil 中,J-Link调试查看变量值总是显示<not in scope>
转载请注明出处,谢谢. MDK/Keil 中,J-Link调试查看变量值总是显示<not in scope> 原因:编译器把代码优化掉了,直接导致在仿真中变量根本没有分配内存,也就无法查看 ...
- python 的 购物小程序
money = input('请输入您的工资:') shop = [("iphone",5800),("ipod",3000),("book" ...
- vue-router核心概念
vue用来实现SPA的插件 使用vue-router 1. 创建路由器: router/index.js new VueRouter({ routes: [ { // 一般路由 path: '/abo ...
- 成都优步uber司机第三组奖励政策
今天成都优步又推出了优步司机第三组,第一二组的奖励大家都晓得,但是第三组的奖励怎么样呢?还是先看看官方给出的消息. 滴滴快车单单2.5倍,注册地址:http://www.udache.com/如何注册 ...