当前位置:网站首页>牛客网:合并区间
牛客网:合并区间
2022-06-22 17:30:00 【lsgoose】

我们来按顺序正常处理一遍就行:
首先按照start从小到大的顺序对数组进行排序,然后我们维护一个答案的数组序列。
对每一个intervals:
如果当前的区间的start比答案序列中最后一个区间的end小或等于,我们更新最后区间的end为原end和新的区间的end的最大值
否则,将新的区间加入答案中
代码如下所示:
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class Solution {
static bool cmp(Interval &a, Interval &b){
return a.start < b.start;
}
public:
vector<Interval> merge(vector<Interval> &intervals) {
vector<Interval> res;
if(intervals.size()==0) return res;
sort(intervals.begin(), intervals.end(), cmp);
res.push_back(intervals[0]);
for(int i=1;i<intervals.size();++i){
if(intervals[i].start <= res.back().end){
res.back().end=max(res.back().end, intervals[i].end);
}else{
res.push_back(intervals[i]);
}
}
return res;
}
};边栏推荐
- 大一女生废话编程爆火!懂不懂编程的看完都拴Q了
- 2022 Chongqing preschool education industry exhibition 𞓜 hi tech Toy Puzzle decompression Toy Expo
- SaaS application development guide
- Array implementation of circular linked list
- Beijing restorer's half moon: how to rekindle the fireworks in store management
- <JVM上篇:内存与垃圾回收篇>08-对象实例化及直接内存
- 每天5分钟玩转Kubernetes | Dashboard典型使用场景
- Custom database connection pool class: requirement: enclose the collection class of a collection object
- 详解openGauss多线程架构启动过程
- [tools] related use of PIP and CONDA
猜你喜欢
![[applet project development -- Jingdong Mall] configuration tabbar & window style for uni app development](/img/cd/bdf26a02a43c63f374861e8431787c.png)
[applet project development -- Jingdong Mall] configuration tabbar & window style for uni app development

Alibaba cloud cannot find the account security group id problem during the account transfer

第四届青年生命科学论坛 | 第一轮通知

大一女生废话编程爆火!懂不懂编程的看完都拴Q了

Explain the startup process of opengauss multithreading architecture in detail

Q: how bad can a programmer be?

SOA面向服务的架构

c# sqlsugar,hisql,freesql orm框架全方位性能测试对比之sqlserver

写一本畅销书是怎样的一种体验

项目经理们在哪个时刻特别想逃离工作?
随机推荐
Binary tree practice the second bullet
数据库行业分析:从全球IT产业趋势到国产数据库发展之路
How does flynk MySQL CDC guarantee the server_ Is the ID globally unique?
Nuxt - Universal(SSR / SSG)/ Single Page App(渲染模式)
Traditional image -- LBP feature
The Fourth Youth Life Science Forum | first round notice
传统图像--LBP特征
Behind the fall of the first Seberg: the extreme race between technology and frostbite
sqlserver保存时遇到这个页面怎么回事啊
Golang implements redis (10): local atomic transactions
Some preliminary explorations of avoiding obstacles and finding paths by rays in unity
jsp连接MySQL总出错
新东方转型双语直播带货给项目管理人的一场课
Database industry analysis: from the global IT industry trend to the development of domestic databases
Introduction to rsps2022 finalist | Dr. Yang Bai
详解openGauss多线程架构启动过程
Complete the sqlsession interface and implementation classes
第四届青年生命科学论坛 | 第一轮通知
List的同步类比较
Golang 實現 Redis(10): 本地原子性事務