[LeetCode] Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals.
Example 1:
Input: [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].
Example 2:
Input: [[1,4],[4,5]]
Output: [[1,5]]
Explanation: Intervals [1,4] and [4,5] are considered overlapping.
NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.
这道和之前那道 Insert Interval 很类似,这次题目要求我们合并区间,之前那题明确了输入区间集是有序的,而这题没有,所以我们首先要做的就是给区间集排序,由于我们要排序的是个结构体,所以我们要定义自己的 comparator,才能用 sort 来排序,我们以 start 的值从小到大来排序,排完序我们就可以开始合并了,首先把第一个区间存入结果中,然后从第二个开始遍历区间集,如果结果中最后一个区间和遍历的当前区间无重叠,直接将当前区间存入结果中,如果有重叠,将结果中最后一个区间的 end 值更新为结果中最后一个区间的 end 和当前 end 值之中的较大值,然后继续遍历区间集,以此类推可以得到最终结果,代码如下:
解法一:
class Solution {
public:
vector<vector<int>> merge(vector<vector<int>>& intervals) {
if (intervals.empty()) return {};
sort(intervals.begin(), intervals.end());
vector<vector<int>> res{intervals[]};
for (int i = ; i < intervals.size(); ++i) {
if (res.back()[] < intervals[i][]) {
res.push_back(intervals[i]);
} else {
res.back()[] = max(res.back()[], intervals[i][]);
}
}
return res;
}
};
下面这种解法将起始位置和结束位置分别存到了两个不同的数组 starts 和 ends 中,然后分别进行排序,之后用两个指针i和j,初始化时分别指向 starts 和 ends 数组的首位置,然后如果i指向 starts 数组中的最后一个位置,或者当 starts 数组上 i+1 位置上的数字大于 ends 数组的i位置上的数时,此时说明区间已经不连续了,我们来看题目中的例子,排序后的 starts 和 ends 为:
starts: 1 8 15
ends: 10 18
红色为i的位置,蓝色为j的位置,那么此时 starts[i+1] 为8,ends[i] 为6,8大于6,所以此时不连续了,将区间 [starts[j], ends[i]],即 [1, 6] 加入结果 res 中,然后j赋值为 i+1 继续循环,参见代码如下:
解法二:
class Solution {
public:
vector<vector<int>> merge(vector<vector<int>>& intervals) {
int n = intervals.size();
vector<vector<int>> res;
vector<int> starts, ends;
for (int i = ; i < n; ++i) {
starts.push_back(intervals[i][]);
ends.push_back(intervals[i][]);
}
sort(starts.begin(), starts.end());
sort(ends.begin(), ends.end());
for (int i = , j = ; i < n; ++i) {
if (i == n - || starts[i + ] > ends[i]) {
res.push_back({starts[j], ends[i]});
j = i + ;
}
}
return res;
}
};
这道题还有另一种解法,这个解法直接调用了之前那道题 Insert Interval 的函数,由于插入的过程中也有合并的操作,所以我们可以建立一个空的集合,然后把区间集的每一个区间当做一个新的区间插入结果中,也可以得到合并后的结果,那道题中的四种解法都可以在这里使用,但是没必要都列出来,这里只选了那道题中的解法二放到这里,代码如下:
解法三:
class Solution {
public:
vector<vector<int>> merge(vector<vector<int>>& intervals) {
vector<vector<int>> res;
for (int i = ; i < intervals.size(); ++i) {
res = insert(res, intervals[i]);
}
return res;
}
vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int> newInterval) {
vector<vector<int>> res;
int n = intervals.size(), cur = ;
for (int i = ; i < n; ++i) {
if (intervals[i][] < newInterval[]) {
res.push_back(intervals[i]);
++cur;
} else if (intervals[i][] > newInterval[]) {
res.push_back(intervals[i]);
} else {
newInterval[] = min(newInterval[], intervals[i][]);
newInterval[] = max(newInterval[], intervals[i][]);
}
}
res.insert(res.begin() + cur, newInterval);
return res;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/56
类似题目:
Interval List Intersections
参考资料:
https://leetcode.com/problems/merge-intervals/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Merge Intervals 合并区间的更多相关文章
- [LeetCode] 56 - Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- LeetCode 56. Merge Intervals 合并区间 (C++/Java)
题目: Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6] ...
- 【LeetCode每天一题】Merge Intervals(合并区间)
Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...
- 056 Merge Intervals 合并区间
给出一个区间的集合, 请合并所有重叠的区间.示例:给出 [1,3],[2,6],[8,10],[15,18],返回 [1,6],[8,10],[15,18].详见:https://leetcode.c ...
- Leetcode56. Merge Intervals合并区间
给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: 区间 [1,3] ...
- 合并区间 · Merge Intervals & 插入区间 · Insert Interval
[抄题]: 给出若干闭合区间,合并所有重叠的部分. 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8, 10] ...
- 【LeetCode】数组--合并区间(56)
写在前面 老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...
- LeetCode: Merge Intervals 解题报告
Merge IntervalsGiven a collection of intervals, merge all overlapping intervals. For example,Given [ ...
- [LeetCode] Merge Intervals 排序sort
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
随机推荐
- [原创]django+ldap+memcache实现单点登录+统一认证
前言 由于公司内部的系统越来越多,为了方便用户使用,通过django进行了单点登录和统一认证的尝试,目前实现了django项目的单点登录和非django项目的统一认证,中间波折挺多,涉及的技术包括dj ...
- 开源分布式数据库中间件MyCat源码分析系列
MyCat是当下很火的开源分布式数据库中间件,特意花费了一些精力研究其实现方式与内部机制,在此针对某些较为重要的源码进行粗浅的分析,希望与感兴趣的朋友交流探讨. 本源码分析系列主要针对代码实现,配置. ...
- passport源码研究
passport的验证过程主要依赖具体的验证策略来实现的,比较常用的有session策略.local策略和github策略等,验证逻辑都是在这些策略类中定义的.passport模块的定 ...
- IOS学习之-私人通讯录
通过一段时间IOS的学习完成了一个简单的应用,"私人通讯录". 运行效果如下图: 1.登录页 2.通讯录列表 3.添加 4.编辑 5.删除 6.注销 总视图结构如下图: 总结本程序 ...
- Nancy之实现API的功能
0x01.前言 现阶段,用来实现API的可能大部分用的是ASP.NET Web API或者是ASP.NET MVC,毕竟是微软官方出产的,用的人也多. 但是呢,NancyFx也是一个很不错的选择.毕竟 ...
- [Winform] DataGridView 总结(FAQ)
Q1. 如何使单元格不可编辑? A:设置 ReadOnly 属性,可以设置的对象包括 DataGridViewRow(行).DataGridViewColumn(列).DataGridViewCel ...
- ASP.NET从MVC5升级到MVC6 RC2 总目录 - 发布在RC2Release之后
序言 随着MVC6RC2的推出,MVC6的脚步越来越近了.但是在我们的手里,有大批量的MVC5的项目.如何将MVC5升级到MVC6,将是一个很大的课题.微软官方暂时没有一个升级指导,或者一个迁移工具, ...
- spring源码:web容器启动(li)
web项目中可以集成spring的ApplicationContext进行bean的管理,这样使用起来bean更加便捷,能够利用到很多spring的特性.我们比较常用的web容器有jetty,tomc ...
- C3p0连接池配置
在Java开发中,使用JDBC操作数据库的四个步骤如下: ①加载数据库驱动程序(Class.forName("数据库驱动类");) ②连接数据库(Connection co ...
- [连载]《C#通讯(串口和网络)框架的设计与实现》- 7.外部接口的设计
目 录 第七章 外部接口的设计... 2 7.1 插件接口... 2 7.2 图形显示接口... 3 7.3 ...