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 ...
随机推荐
- Liunx创建到部署ASP.NET Core项目从零开始-----使用Centos7
一.搭建环境 1..注册Microsoft密钥和源 执行命令:sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages ...
- MySQL 高可用之主从复制
MySQL主从复制简介 Mysql的主从复制方案,都是数据传输的,只不过MySQL无需借助第三方工具,而是自带的同步复制功能,MySQL的主从复制并不是磁盘上文件直接同步,而是将binlog日志发送给 ...
- 本地Linux虚拟机内网穿透,服务器文件下载到本地磁盘
本地Linux虚拟内网穿透 把服务器文件下载到本地磁盘 https://natapp.cn/ 1.注册账户点击免费隧道
- springcloud 依赖版本问题
SpringCloud 版本: 版本名称 版本 Finchley snapshot版 Edgware snapshot版 Dalston SR1 当前最新稳定版本 Camden SR7 稳定版本 Br ...
- CUDA学习(三)之使用GPU进行两个数相加
在CPU上定义两个数并赋值,然后使用GPU核函数将两个数相加并返回到CPU,在CPU上显示 #include "cuda_runtime.h" #include "dev ...
- what is muxing and demuxing
They're short for multiplexing and demultiplexing. Multiplexing means combining different types of d ...
- SpringMVC 进阶
请求限制 一些情况下我们可能需要对请求进行限制,比如仅允许POST,GET等... RequestMapping注解中提供了多个参数用于添加请求的限制条件 value 请求地址 path 请求地址 m ...
- Codeforces_801
A.直接暴力就行了,先把能组合的按线性组合掉,再枚举剩下相邻没用过的. #include<bits/stdc++.h> using namespace std; string s; ] = ...
- centos系统组件优化
CentOS Linux在公司服务器上广泛被使用,本文总结了一些常见的加固方法. 基本原则: 最小的权限+最小的服务=最大的安全 操作之前先备份: 为避免配置错误无法登录主机,请始终保持有一个终端已用 ...
- Java 中序列化与反序列化
一. 序列化和反序列化概念 Serialization(序列化)是一种将对象以一连串的字节描述的过程:反序列化deserialization是一种将这些字节重建成一个对象的过程.将程序中的对象,放入文 ...