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. Bootstrap:教程、简介、环境安装

    ylbtech-Bootstrap:教程.简介.环境安装 1. Bootstrap 教程返回顶部 1. Bootstrap 教程 Bootstrap,来自 Twitter,是目前最受欢迎的前端框架.B ...

  2. 自绘图片下拉项 combobox listbox

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  3. jquey XMLHttpRequest cannot load url.Origin null is not allowed by Access-Control-Allow-Origin

    此篇文章原文地址:http://blog.csdn.net/wangsky2/article/details/22961345 正文: 原文地址:http://stackoverflow.com/qu ...

  4. phpexcel导入数据出现PHPExcel_RichText Object解决办法

    在导入excel的时候会出现异常情况,有的问题出现PHPExcel_RichText object,错误代码如下 PHPExcel_RichText Object ( [_richTextElemen ...

  5. PHP mysqli_autocommit() 函数

    定义和用法 mysqli_autocommit() 函数开启或关闭自动提交数据库修改. 提示:请查看 mysqli_commit() 函数,用于提交指定数据库连接的当前事务.请查看 mysqli_ro ...

  6. linux主机555、644、666、755、777权限详解

    linux主机555.644.666.755.777权限详解 发表时间:2014-06-03 05:07 来源:未知 分类:其它代码 作者:岑溪网站开发 点击:次 linux主机555.644.666 ...

  7. CUDA C Programming Guide 在线教程学习笔记 Part 10【坑】

    ▶ 动态并行. ● 动态并行直接从 GPU 上创建工作,可以减少主机和设备间数据传输,在设备线程中调整配置.有数据依赖的并行工作可以在内核运行时生成,并利用 GPU 的硬件调度和负载均衡.动态并行要求 ...

  8. sed 小结

    语法格式1: sed option command file 注意区分 option command, option 以-开始 command 以单?引号包围 ———— 这完全是误会, 我测试好多后发 ...

  9. 39. 拼接表字段b.day

    var fun = ABS_LOADBEAN("com.plug.FunctionHelper");//var v_div = fun.funHelper.strAdd(" ...

  10. 使用Apache POI处理excel公式不更新的解决办法

    使用poi更新excel时,如果单元格A设置了公式,当其依赖的其他单元格填充了值之后,导出的excel中A仍为公式而不是自动计算的值,如图: Paste_Image.png 分值小计没有更新成计算结果 ...