当前位置:网站首页>Niuke.com: consolidation interval
Niuke.com: consolidation interval
2022-06-22 19:03:00 【lsgoose】

Let's deal with it normally in sequence :
First of all, in accordance with the start Sort the array from small to large , Then we maintain an array sequence of answers .
For each intervals:
If the current interval is start Than the last interval in the answer sequence end Small or equal to , We update the last interval end Original end And the new interval end The maximum of
otherwise , Add a new range to the answer
The code is as follows :
/**
* 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;
}
};边栏推荐
- 平安证券开户安全吗?它和平安银行是什么关系呢?
- 不断重修的计划与变化
- Five practical tips for power Bi (complimentary books at the end of the article)
- Preliminary controller input of oculus learning notes (1)
- 请问一下,CDC2.0在读取mysql的时候,应该是没有表锁了吧,刚刚报了一个错误,access d
- 新人报道的笔记
- UE4_UE5制作3DUI-跟随相机朝向(附工程)
- 牛客网:最小覆盖子串
- Postman learning
- Beijing restorer's half moon: how to rekindle the fireworks in store management
猜你喜欢
传输层 知识点总结

Cookie加密3+RPC解法

Activity start process sorting

Some preliminary explorations of avoiding obstacles and finding paths by rays in unity

What happened to this page when sqlserver was saving

Power BI的五个实用小技巧(文末赠书)

2022 G2 power plant boiler stoker question bank and online simulation examination

In May, 2022, China's game manufacturers and applications went to sea, with top 30 revenue in EMEA region

Grafana 9 正式发布,更易用,更酷炫了!

巴比特 | 元宇宙每日必读:传腾讯成立XR部门,元宇宙板块再次上涨,多家券商发报告关注虚拟人的投资机会...
随机推荐
RSPS2022 Finalist | Dr. Yang Bai 简介
Which securities company is better to open an account when making an appointment to play new bonds? It is safer to open an account
Jenkins容器安装ruby-runtime插件失败报错解决
exness整理马斯克收购推特需要解决三个问题
线程池:ThreadPoolExcutor源码阅读
Beijing restorer's half moon: how to rekindle the fireworks in store management
C sqlsugar, hisql, FreeSQL ORM framework omni-directional performance test comparison sqlserver
Activity start process sorting
Cookie加密3+RPC解法
【win11】注册表修改fix 右键没有新建
[OWT] OWT client native P2P E2E test vs2017 build
Excuse me, when cdc2.0 reads mysql, there should be no table lock. An error was just reported, access D
游戏NFT市场:OpenSea最易被切下的蛋糕
Azkaban startup error 2022/06/20 21:39:27.726 +0800 error [stdouterrredirect] [azkaban] exception in thread "m
新东方转型双语直播带货给项目管理人的一场课
Postman learning
Is it safe for Ping An Securities to open an account? What is its relationship with Ping An Bank?
Golang实现基于Redis的可靠延迟队列
Set of redis data structure
不断重修的计划与变化