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)的更多相关文章

  1. 【LeetCode】57. Insert Interval [Interval 系列]

    LeetCode中,有很多关于一组interval的问题.大体可分为两类: 1.查看是否有区间重叠: 2.合并重叠区间;  3.插入新的区间: 4. 基于interval的其他问题 [ 做题通用的关键 ...

  2. leetcode 56. Merge Intervals 、57. Insert Interval

    56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...

  3. 56. Merge Intervals 57. Insert Interval *HARD*

    1. Merge Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[ ...

  4. 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 ...

  5. 【LeetCode】57. Insert Interval

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

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

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

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

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

  8. [leetcode sort]57. Insert Interval

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

  9. 57. Insert Interval

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

随机推荐

  1. selenium java-2 chrome driver与对应版本

    chrome driver下载地址:https://npm.taobao.org/mirrors/chromedriver driver与chrome的对应关系: 1.进入最新的driver,查看no ...

  2. 用HTML5播放IPCamera视频

    HTML5增加了vedio标签,能直接播放视频,但是播放的格式是有限的. 这里将IPCamera的视频转换OGG格式,再通过HTML5网页播放,播放还是很清晰的.IPCamera要支持RTSP等视频流 ...

  3. ue4 多相机分屏与小地图效果实现教程

    转自:http://blog.csdn.net/shenmifangke/article/details/51940007  通过使用ue4的UI和rendertarget来实现 优点就是可以随意设置 ...

  4. javascript节点操作移出节点removeChild()

    removeChild(a)是用来删除文档中的已有元素 参数a:要移出的节点 <div id="guoDiv"> <span>1</span> ...

  5. Linux 下的两种分层存储方案

    背景介绍 随着固态存储技术 (SSD),SAS 技术的不断进步和普及,存储介质的种类更加多样,采用不同存储介质和接口的存储设备的性能出现了很大差异.SSD 相较于传统的机械硬盘,由于没有磁盘的机械转动 ...

  6. hive数据导入导出和常用操作

    导出到本地文件 insert overwrite local directory '/home/hadoop'select * from test1; 导出到hdfs insert overwrite ...

  7. Flash和滚动字幕

    flash 1.插入flash     1)<object>             <embed src="路径"></embed>      ...

  8. C# 程序A发送Log记录给程序B,程序B处理和分析Log记录

    C# 程序A发送Log记录给程序B,程序B处理和分析Log记录 关键字:C# ;Log记录 ;在线Log记录;Socket:httplistener 一.常用场景 1. APP开发,在真机或者虚拟机上 ...

  9. vim编程设置

    在终端下使用vim进行编辑时,默认情况下,编辑的界面上是没有显示行号.语法高亮度显示.智能缩进 等功能的.为了更好的在vim下进行工作,需要手动设置一个配置文件:.vimrc.在启动vim时,当前用户 ...

  10. leetcode530

    /** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...