LeetCode 57. Insert Interval 插入区间 (C++/Java)
题目:
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.
分析:
给定一个无重叠的,已排序的区间集合,要求在集合中插入一个新的区间,保证插入后区间无重叠且有序。
LeetCode 56. Merge Intervals 合并区间 (C++/Java)的进阶,我们可以利用56题的解法来解决这道题,也就是在原列表中按序将新区间插入,然后再进行区间合并。
此外我们还可以将原集合区间分为三部分,一部分是右端点小于新区间左端点,一部分是左端点大于新区间右端点,这两部分区间不会和新区间发生合并,剩下的区间则需要和新区间合并,遍历完所有区间后,将三部分合并即可。
C++采用插入排序合并,Java采用分区合并
程序:
C++
class Solution {
public:
vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
auto it = intervals.begin();
while(it != intervals.end() && (*it)[] < newInterval[])
it++;
intervals.insert(it, newInterval);
vector<vector<int>> res;
for(auto interval:intervals){
if(res.empty())
res.push_back(interval);
else if(res.back()[] < interval[])
res.push_back(interval);
else{
auto t = res.back();
res.pop_back();
res.push_back({t[], max(t[], interval[])});
}
}
return res;
}
};
Java
class Solution {
public int[][] insert(int[][] intervals, int[] newInterval) {
int start = newInterval[0];
int end = newInterval[1];
List<int[]> l = new ArrayList<>();
List<int[]> r = new ArrayList<>();
for(int[] interval:intervals){
if(interval[1] < start){
l.add(interval);
}else if(end < interval[0]){
r.add(interval);
}else{
start = Math.min(start, interval[0]);
end = Math.max(end, interval[1]);
}
}
l.add(new int[]{start, end});
l.addAll(r);
return l.toArray(new int[l.size()][]);
}
}
LeetCode 57. Insert Interval 插入区间 (C++/Java)的更多相关文章
- 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插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [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 (HARD)
Insert interval 题意简述:给定若干个数轴上的闭区间,保证互不重合且有序,要求插入一个新的区间,并返回新的区间集合,保证有序且互不重合. 只想到了一个线性的解法,所有区间端点,只要被其 ...
- 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 ...
- [LeetCode] 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: 57. Insert Interval(Hard)
1. 原题链接 https://leetcode.com/problems/insert-interval/description/ 2. 题目要求 该题与上一题的区别在于,插入一个新的interva ...
随机推荐
- Vim学习之路1
与之前的随笔一样,这个也是记录Vim常用命令以供日后查找所用.对于Vim,简介而又功能强大,学习之后代码书写相当愉快. 1. 保存并退出 :wq 2. 进入标准插入模式退出命令模式 i 3. 退出标准 ...
- 网络io模型总结
操作系统基本概念 首先来来说下操作系统,嗯,操作系统是计算机硬件的管理软件,是对计算机硬件的抽象,操作系统将应用程序分为用户态和内核态,例如驱动程序就位于内核态,而我们写的一般程序都是用户态,包括we ...
- android开发实战-记账本APP(二)
继昨天的开发,继续完成今天的内容. (一)开始构建一些业务逻辑,开始构建记账本的添加一笔记账的功能. ①对fab按钮的click时间进行修改,创建一个AlertDialog.Builder对象,因此我 ...
- 如何理解 HTMLTestRunner 中 test (result)?UnitTest是如何运行的?
我们在用Unittest框架时,生成html格式的报告一般都是用HTMLTestRunner.py这个第三方库,大概使用方法如下: with open(config.report_file, 'wb' ...
- alert弹出窗口,点击确认后关闭页面
alert("点击确认后,关闭页面"); window.opener=null;window.top.open('','_self','');window.close(this);
- Linux学习笔记-centos查看版本号和内核信息
1.查看centos系统版本号: 打开终端窗口: cat /etc/redhat-release 2.查看Linux内核版本信息: uname -a 或者 在图形化桌面右上角点开设置,在设置窗口选择详 ...
- Struts(四)
1.Struts 2提供了非常强大的类型转换功能,提供了多种内置类型转换器,也支持开发自定义类型转换器2.Struts 2框架使用OGNL作为默认的表达式语言 ==================== ...
- The import java.io cannot be resolved (类库无法解析的问题解决 )
导入一个新项目后常会出现 The import java.io cannot be resolved String cannot be resolved to a type 其原因在于没有导入需要的包 ...
- java核心技术----Object类
package java.lang; /** * Class {@code Object} is the root of the class hierarchy. * Every class has ...
- python strip()方法使用
描述 python strip() ,用于去除述字符串头尾指定字符(默认为空格或换行符)或字符序列. 注意:此方法只能去除头尾的空格或是换行符,不能去除中间的. 语法: str.strip([char ...