当前位置:网站首页>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;
}
};边栏推荐
- Mysql如何删除数据库表中的某一列
- 阻碍华为5G手机的关键芯片取得突破,国产芯片已取得一成份额
- 腾讯云国际版云服务器欠费说明
- Azkaban startup error 2022/06/20 21:39:27.726 +0800 error [stdouterrredirect] [azkaban] exception in thread "m
- Redis usage scenario sharing (project practice)
- Live broadcast Preview - 12 first-class Chinese scholars open ICLR 2022
- Postman learning
- 2022焊工(初级)特种作业证考试题库模拟考试平台操作
- Grafana 9 is officially released, which is easier to use and more cool!
- postman学习
猜你喜欢

Killed by the script, and "resurrected" by camping
Complete the sqlsession interface and implementation classes

wpa_cli参数说明

2022年R2移动式压力容器充装试题模拟考试平台操作

@Lucky user of "Qilu Duojiao", Shandong 5A scenic spot calls you to visit the park for free!
DBMS in Oracle_ output. put_ Example of line usage

SystemVerilog(十二)-$unit声明空间

Live streaming: dongyuhui is so popular. Is there anyone watching liuzhenhong?

Nuxt - create nuxt app

At 19:30 today, the science popularization leader said that he would take you to explore how AI can stimulate human creativity
随机推荐
Exness sorted out three problems to be solved in Musk's acquisition of Twitter
写一本畅销书是怎样的一种体验
直播预告 | 12位一作华人学者开启 ICLR 2022
Azkaban startup error 2022/06/20 21:39:27.726 +0800 error [stdouterrredirect] [azkaban] exception in thread "m
Traditional image -- LBP feature
Redis usage scenario sharing (project practice)
牛客网:判断是否为回文字符串
Live streaming: dongyuhui is so popular. Is there anyone watching liuzhenhong?
@“齐鲁多娇”幸运用户,山东5A景区喊你免费游园啦!
@Lucky user of "Qilu Duojiao", Shandong 5A scenic spot calls you to visit the park for free!
Dynamically changing the style of label elements in a loop
What happened to this page when sqlserver was saving
Killed by the script, and "resurrected" by camping
Oracle中dbms_output.put_line的用法实例
平安证券开户安全吗?它和平安银行是什么关系呢?
Is it safe for Ping An Securities to open an account? What is its relationship with Ping An Bank?
Unity中通过射线躲避障碍物寻路的一些初步探索
数组实现循环链表
Filebeat collects log data and transfers it to redis. Different es indexes are created based on log fields through logstash
自定义数据库连接池类: 要求:封闭一个Collection对象的集合类