57. Insert Interval (Array; Sort)
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:
Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].
Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].
This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10].
思路:和上一题差不多,但要考虑更多问题,注意不要漏掉newInterval可能整体插入(不做merge)的情况
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class Solution {
public:
vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {
if(intervals.empty()){
intervals.push_back(newInterval);
return intervals;
} //first find the fist end >= newInterval.start
vector<Interval>::iterator startInsertPos = intervals.begin();
for(; startInsertPos < intervals.end(); startInsertPos++){
if(startInsertPos->end >= newInterval.start) break;
}
if(startInsertPos == intervals.end()){ //insert in the final
intervals.push_back(newInterval);
return intervals;
} //find the last start <= newInterval.end
vector<Interval>::iterator endInsertPos = startInsertPos;
for(; endInsertPos < intervals.end(); endInsertPos++){
if(endInsertPos->start > newInterval.end){
break;
}
}
endInsertPos--; //intervals between [startInsertPos, endInsertPos] may need to be merged
//case 1: insert before startInsertPos
if(startInsertPos->start > newInterval.end){
intervals.insert(startInsertPos,newInterval);//insert in the position startInserPos
}
//case2: insert after endInsertPos
else if(endInsertPos->end < newInterval.start){
intervals.insert(endInsertPos+,newInterval);//insert in the position endInsertPos+1
}
//case3: merge
else{
startInsertPos->start = min(newInterval.start, startInsertPos->start);
startInsertPos->end = max(newInterval.end, endInsertPos->end);
intervals.erase(startInsertPos+,endInsertPos+);//erase the elem from startInserPos+1 to endInsertPos
}
return intervals;
57. Insert Interval (Array; Sort)的更多相关文章
- 【LeetCode】57. Insert Interval [Interval 系列]
LeetCode中,有很多关于一组interval的问题.大体可分为两类: 1.查看是否有区间重叠: 2.合并重叠区间; 3.插入新的区间: 4. 基于interval的其他问题 [ 做题通用的关键 ...
- leetcode 56. Merge Intervals 、57. Insert Interval
56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...
- 56. Merge Intervals 57. Insert Interval *HARD*
1. Merge Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[ ...
- 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】57. Insert Interval
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- leetCode 57.Insert Interval (插入区间) 解题思路和方法
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ...
- 第一周 Leetcode 57. Insert Interval (HARD)
Insert interval 题意简述:给定若干个数轴上的闭区间,保证互不重合且有序,要求插入一个新的区间,并返回新的区间集合,保证有序且互不重合. 只想到了一个线性的解法,所有区间端点,只要被其 ...
- [leetcode sort]57. Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- 57. Insert Interval
题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...
随机推荐
- PAT 乙级 1037 在霍格沃茨找零钱(20)C++版
1037. 在霍格沃茨找零钱(20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 如果你是哈利·波特迷,你会知 ...
- [转] Maven.pom.xml 配置示例
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- 阿里云启用IPV6
ping过别人的IPv6网址之后,可以确定,局域网是不支持IPv6的.所以要使用隧道技术建立两台机器之间的IPv6连接 1.发现测试用服务器上没有IPv6地址.所以测试服务器的内核应该是没有IPv6模 ...
- sqoop操作之ORACLE导入到HIVE
导入表的所有字段 sqoop import --connect jdbc:oracle:thin:@192.168.1.107:1521:ORCL \ --username SCOTT --passw ...
- python 命名元组(namedtuple)
我们知道c/c++语言中,有结构体这种数据类型: struct{ string name; int age; char sex; }student; 在对结构体对象进行赋值或者取值时可以使用.运算 ...
- mysql 的安装,密码及修改 ,权限,基础语句(增删改查)
参考网址:https://www.cnblogs.com/majj/p/9160383.html (安装等) https://www.cnblogs.com/majj/p/9160421.htm ...
- C语言中字符串存储方法
众所周知,C语言中没有数据类型能够存储字符串, char数据类型仅仅能够存储一个字符的数据,那么在C语言中关于存储字符串这一难题我们改何去何从呢? 下面将详述相关的字符串存储方法; 1,使用字符数组存 ...
- 18.scrapy中selector的用法
Selector是一个独立的模块. Selector主要是与scrapy结合使用的. 开启Scrapy shell: 1.打开命令行cmd 2.scrapy shell http://doc.scra ...
- HTML5 Canvas ( 图形的像素操作 ) getImageData, putImageData, ImgData.data
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- angular controller 之间的通信方式
AngularJS中的controller是个函数,用来向视图的作用域($scope)添加额外的功能,我们用它来给作用域对象设置初始状态,并添加自定义行为. 当我们在创建新的控制器时,angularJ ...