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

类似题目:

Range Module

Merge Intervals

参考资料:

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

https://leetcode.com/problems/insert-interval/discuss/21676/Clean-and-short-Java-solution-with-explanation

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 57. Insert Interval 插入区间的更多相关文章

  1. LeetCode 57. Insert Interval 插入区间 (C++/Java)

    题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...

  2. leetCode 57.Insert Interval (插入区间) 解题思路和方法

    Insert Interval  Given a set of non-overlapping intervals, insert a new interval into the intervals ...

  3. [leetcode]57. Insert Interval插入区间

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  4. [LeetCode] 57. Insert Interval 解决思路

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  5. 第一周 Leetcode 57. Insert Interval (HARD)

    Insert interval  题意简述:给定若干个数轴上的闭区间,保证互不重合且有序,要求插入一个新的区间,并返回新的区间集合,保证有序且互不重合. 只想到了一个线性的解法,所有区间端点,只要被其 ...

  6. leetcode 57 Insert Interval & leetcode 1046 Last Stone Weight & leetcode 1047 Remove All Adjacent Duplicates in String & leetcode 56 Merge Interval

    lc57 Insert Interval 仔细分析题目,发现我们只需要处理那些与插入interval重叠的interval即可,换句话说,那些end早于插入start以及start晚于插入end的in ...

  7. [LeetCode] Insert Interval 插入区间

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  8. 057 Insert Interval 插入区间

    给出一个无重叠的按照区间起始端点排序的区间列表.在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间).示例 1:给定区间 [1,3],[6,9],插入并合并 ...

  9. Leetcode#57 Insert Interval

    原题地址 遍历每个区间intervals[i]: 如果intervals[i]在newInterval的左边,且没有交集,把intervals[i]插入result 如果intervals[i]在ne ...

随机推荐

  1. tensorboard--打开训练的日志文件

    tensorboard --logdir=logs 注意:等号之间不要空格.

  2. 基于OceanStor Dorado V3存储之精简高效 Smart 系列特性

    基于OceanStor Dorado V3存储之精简高效 Smart 系列特性 1.1  在线重删 1.2  在线压缩 1.3  智能精简配置 1.4  智能服务质量控制 1.5  异构虚拟化 1.6 ...

  3. MySQL for OPS 02:SQL 基础

    写在前面的话 上一节主要谈谈 MySQL 是怎么安装的以及最简单的初始化我们应该做哪些配置.其中也用到了一些简单的用户操作 SQL,所以这一节主要学习常用的 SQL 使用. SQL 介绍 在了解 SQ ...

  4. 2019-6-15-WPF-触摸到事件

    原文:2019-6-15-WPF-触摸到事件 title author date CreateTime categories WPF 触摸到事件 lindexi 2019-06-15 08:58:54 ...

  5. identityServer3+ADFS实现域用户登录授权

    准备: ADFS安装配置 https://www.cnblogs.com/luoyedemeng/articles/9837685.html 添加一个Providers private void Co ...

  6. U盘安装CentOS 7提示 “Warning: /dev/root does not exist, could not boot” 解决办法

    1.把U盘的Lable(即标签)修改成centos 2.在安装界面上按TAB键,修改启动路径,把”CENTOS\x207\x20x86_64″改成 “centos”

  7. php字符串查找函数 php查找字符串中出现的次数函数substr_count,判断字符串中是否包含另一个字符串函数strpos

    php字符串查找函数 php查找字符串中出现的次数函数substr_count,判断字符串中是否包含另一个字符串函数strpossubstr_count($haystack, $needle [,$o ...

  8. jquery根据下拉框选择的值显示输入框

    原理就是根据下拉框选择的值来控制显示那个输入框: html代码: 首先定义一个下拉框,$serviceTypeList就是后台传过来的所有属性, <div class="uk-form ...

  9. 【转】Git使用教程之远程仓库

    1.远程仓库 在了解之前,先注册github账号,由于你的本地Git仓库和github仓库之间的传输是通过SSH加密的,所以需要一点设置: 第一步:创建SSH Key.在用户主目录下,看看有没有.ss ...

  10. XGBoost 完整推导过程

    参考: 陈天奇-"XGBoost: A Scalable Tree Boosting System" Paper地址: <https://arxiv.org/abs/1603 ...