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 ...
随机推荐
- beta版本发布说明与总结
1.发布说明: 软件介绍: deta版本的发布最终是一个可安装使用的窗体程序,已经由Alpha版本的应用解决方案完成到一个程序: deta版本解决了Alpha版本遗留的软件技术方面错误问题,以及针对有 ...
- 解决 C# GetPixel 和 SetPixel 效率问题(转)
在对Bitmap图片操作的时候,有时需要用到获取或设置像素颜色方法:GetPixel 和 SetPixel, 如果直接对这两个方法进行操作的话速度很慢,这里我们可以通过把数据提取出来操作,然后操作完在 ...
- 每天玩转3分钟 MyBatis-Plus - 6. select 用法
每天玩转3分钟 MyBatis-Plus - 1. 配置环境 每天玩转3分钟 MyBatis-Plus - 2. 普通查询 每天玩转3分钟 MyBatis-Plus - 3. 高级查询(一) 每天玩转 ...
- Android教程2020 - RecyclerView使用入门
本文介绍RecyclerView的使用入门.这里给出一种比较常见的使用方式. Android教程2020 - 系列总览 本文链接 想必读者朋友对列表的表现形式已经不再陌生.手机上有联系人列表,文件列表 ...
- linux--->阿里云centos6.9环境配置安装lnmp
阿里云centos6.9环境配置安装lnmp mysql安装 本人博客:http://www.cnblogs.com/frankltf/p/8615418.html PHP安装 1.安装依赖关系 yu ...
- Linux 常用工具iptables
iptables简介 netfilter/iptables(简称为iptables)组成Linux平台下的包过滤防火墙,与大多数的Linux软件一样,这个包过滤防火墙是免费的,它可以代替昂贵的商业防火 ...
- JDK源码之Byte类分析
一 简介 byte,即字节,由8位的二进制组成.在Java中,byte类型的数据是8位带符号的二进制数,以二进制补码表示的整数 取值范围:默认值为0,最小值为-128(-2^7);最大值是127(2^ ...
- Java的变量与常量
常量: 在程序运行期间,固定不变得量. 常量的分类: 字符串常量:凡是用双引号引起来的部分,叫做字符串常量.例如:“abc”.“Hello”.“123”. 整数常量:直接写上的数字,没有小数点.例如: ...
- 学习Qt的资源-网站、论坛、博客等
来自<零基础学Qt 4编程>一书的附录 附录C Qt资源 C.1 Qt 官方资源 全球各大公司以及独立开发人员每天都在加入 Qt 的开发社区.他们已经认识到了Qt 的架构本身便可加快应用程 ...
- ARTS Week 1
Oct 28,2019 ~ Nov 3,2019 Algorithm 本周的学习的算法是二分法.二分法可以用作查找即二分查找,也可以用作求解一个非负数的平方根等.下面主要以二分查找为例. 为了后续描述 ...