[LeetCode] Insert Interval 插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
You may assume that the intervals were initially sorted according to their start times.
Example 1:
Input: intervals = [[1,3],[6,9]], newInterval = [2,5]
Output: [[1,5],[6,9]]
Example 2:
Input: intervals =[[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval =[4,8]
Output: [[1,2],[3,10],[12,16]]
Explanation: Because the new interval[4,8]overlaps with[3,5],[6,7],[8,10].
NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.
这道题让我们在一系列非重叠的区间中插入一个新的区间,可能还需要和原有的区间合并,可以对给定的区间集进行一个一个的遍历比较,那么会有两种情况,重叠或是不重叠,不重叠的情况最好,直接将新区间插入到对应的位置即可,重叠的情况比较复杂,有时候会有多个重叠,需要更新新区间的范围以便包含所有重叠,之后将新区间加入结果 res,最后将后面的区间再加入结果 res 即可。具体思路是,用一个变量 cur 来遍历区间,如果当前 cur 区间的结束位置小于要插入的区间的起始位置的话,说明没有重叠,则将 cur 区间加入结果 res 中,然后 cur 自增1。直到有 cur 越界或有重叠 while 循环退出,然后再用一个 while 循环处理所有重叠的区间,每次用取两个区间起始位置的较小值,和结束位置的较大值来更新要插入的区间,然后 cur 自增1。直到 cur 越界或者没有重叠时 while 循环退出。之后将更新好的新区间加入结果 res,然后将 cur 之后的区间再加入结果 res 中即可,参见代码如下:
解法一:
class Solution {
public:
vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
vector<vector<int>> res;
int n = intervals.size(), cur = ;
while (cur < n && intervals[cur][] < newInterval[]) {
res.push_back(intervals[cur++]);
}
while (cur < n && intervals[cur][] <= newInterval[]) {
newInterval[] = min(newInterval[], intervals[cur][]);
newInterval[] = max(newInterval[], intervals[cur][]);
++cur;
}
res.push_back(newInterval);
while (cur < n) {
res.push_back(intervals[cur++]);
}
return res;
}
};
下面这种方法的思路跟上面的解法很像,只不过没有用 while 循环,而是使用的是 for 循环,但是思路上没有太大的区别,变量 cur 还是用来记录新区间该插入的位置,稍有不同的地方在于在 for 循环中已经将新区间后面不重叠的区间也加进去了,for 循环结束后就只需要插入新区间即可,参见代码如下:
解法二:
class Solution {
public:
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;
}
};
下面这种解法就是把上面解法的 for 循环改为了 while 循环,其他的都没有变,代码如下:
解法三:
class Solution {
public:
vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
vector<vector<int>> res;
int n = intervals.size(), cur = , i = ;
while (i < n) {
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][]);
}
++i;
}
res.insert(res.begin() + cur, newInterval);
return res;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/57
类似题目:
参考资料:
https://leetcode.com/problems/insert-interval/
https://leetcode.com/problems/insert-interval/discuss/21669/Easy-and-clean-O(n)-C++-solution
https://leetcode.com/problems/insert-interval/discuss/21602/Short-and-straight-forward-Java-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Insert Interval 插入区间的更多相关文章
- leetCode 57.Insert Interval (插入区间) 解题思路和方法
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ...
- [LeetCode] 57. Insert Interval 插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- LeetCode 57. Insert Interval 插入区间 (C++/Java)
题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...
- [leetcode]57. Insert Interval插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- 057 Insert Interval 插入区间
给出一个无重叠的按照区间起始端点排序的区间列表.在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间).示例 1:给定区间 [1,3],[6,9],插入并合并 ...
- leetcode Insert Interval 区间插入
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Insert Interval 使用模拟 ...
- [LeetCode]Insert Interval 考虑多种情况
写太复杂了. 思想:确定带插入区间的每一个边界位于给定区间中的哪个位置,共同拥有5种情况 -1 |(0)_1_(2)| (3) 当中.0,1,2这三种情况是一样的. 确定每一个带插入区间的两个边界分 ...
- [leetcode]Insert Interval @ Python
原题地址:https://oj.leetcode.com/problems/insert-interval/ 题意: Given a set of non-overlapping intervals, ...
- Leetcode Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
随机推荐
- 高仿QQ顶部控件之IOS SegmentView
经常会看到QQ上面有一个 消息和电话 的顶部面板,这个空间是IOS7的分段控制,android中没有这个控件,今天在威哥的微信公众号中成功gank到这个自定义控件的实现,下面跟着尝试一波. 首先是定义 ...
- error RC1015: cannot open include file 'afxres.h' 解决办法
在为WindowsPhone8程序添加本地化的过程中遇到这个问题: 问题原因就是afxres.h文件缺失,下载它,放到VS安装目录下的VS\include目录下就可以了(选择目录的时候注意对应对版本) ...
- C# 视频编辑
VidCoder VidCoder是一个开源免费的DVD/蓝光视频抓取和转码软件.使用HandBrake做为编码引擎.比Handbrake拥有更友好的用户界面. 可裁剪.剪切.字幕编辑.转码等. 官网 ...
- C# 高效编程笔记2
C# 高效编程笔记2 1.理解GetHashCode()的陷阱 (1)作用:作为基于散列集合定义键的散列值,如:HashSet<T>,Dictionary<K,V>容器等 (2 ...
- 数据结构:队列 链表,顺序表和循环顺序表实现(python版)
链表实现队列: 尾部 添加数据,效率为0(1) 头部 元素的删除和查看,效率也为0(1) 顺序表实现队列: 头部 添加数据,效率为0(n) 尾部 元素的删除和查看,效率也为0(1) 循环顺序表实现队列 ...
- python之路目录
python开发[第一篇]:目录 python开发[第二篇]:python初体验 python开发[第三篇]:python基础之条件控制与循环 python开发[第四篇]:python基础之运算符 p ...
- GJM :Unity使用EasyAR实现脱卡功能
首先说下大致思路当卡片离开摄像头时间,ImageTarget-Image的SetActive (false),所以其子物体(model)也就不显示了,因此解决的办法就是在Target (false)时 ...
- 【CLR via C#】CSC将源代码编译成托管模块
下图展示了编译源代码文件的过程.如图所示,可用支持 CLR 的任何一种语言创建源代码文件.然后,用一个对应的编译器检查语法和分析源代码.无论选用哪一个编译器,结果都是一个托管模块(managedmod ...
- java基础2.-------interface接口类,实现接口
1.为什么使用接口,是把功能方法都写在一个类中,在其他需要调用的时候,通过implements实现该接口 2.接口可以继承多个父类接口,在实现接口的时候,实现类实现所有方法 3.在接口类写方法时,自动 ...
- poi读取excel模板,填充内容并导出,支持导出2007支持公式自动计算
/** * 版权所有(C) 2016 * @author www.xiongge.club * @date 2016-12-7 上午10:03:29 */ package xlsx; /** * @C ...