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. 1082 Read Number in Chinese (25 分)

    1082 Read Number in Chinese (25 分) Given an integer with no more than 9 digits, you are supposed to ...

  2. 1009 Product of Polynomials (25 分)

    1009 Product of Polynomials (25 分) This time, you are supposed to find A×B where A and B are two pol ...

  3. td高度不随内容变化display:block;display:block;display:block;display:block;display:block;

    在TD里加个DIV就可以解决!CSS对应改成#aaa td div{ height:236px; overflow:hidden; 在TD里加个DIV就可以解决!CSS对应改成#aaa td div{ ...

  4. Node.js 介绍及安装

    Node.js是一个Javascript运行环境(runtime environment),发布于2009年5月,由Ryan Dahl开发,实质是对Chrome V8引擎进行了封装.本文详细介绍了No ...

  5. django (文件操作)

    文件传输html中 form表单必须写 文件的一个属性 enctype="multipart/form-data" file_obj = request.FILES.get('fi ...

  6. IntelliJ IDEA神器使用技巧笔记

    1. Alt + 数字 打开idea 快捷键打开相应的窗口: 高效定位代码: 无处不在的跳转 1.项目间的跳转: Windows ->   ctrl+alt+[   /  ] 2.文件之间的跳转 ...

  7. C# window Service实现调用有UI的应用程序(关于win xp以后的window系统)

    我开发的系统中有一接口程序(这里就称Task,是一个C#的Console Application)经常无故的死掉,导致第二天的数据不能正常解析,所以,我写了一个window service去监视Tas ...

  8. 0_Simple__UnifiedMemoryStreams

    使用 OpenMP 和 pthreads 两种环境,利用实现统一内存编址,计算基本的矩阵乘法 result = α * A * x + β * result . ▶ 源代码 #include < ...

  9. 15. "wm_concat"_数据库中将查询出来的多条记录中的某个字段用","拼接起来

    例子: select wm_concat(roleid) from lbmember where userid = ? 值的形式:1,2,3 下面是把1,2,3转换为1;2;3select repla ...

  10. XE4 TStringDynArray 比 c6 的TStringList 好用 字符串 分解 分割 转换 TByteDynArray

    TStringDynArray 动态数组  字符串 分解 分割  System::DynamicArray<System::UnicodeString> TByteDynArray,    ...