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

问题:给定一个区间数组,插入一个新的区间元素。区间数组已经按照 区间开始值升序排序。

思路:

1. 找到左边第一个 intervals[i].end >= newInterval.start 的元素。

2. 若 intervals[i].start > newInterval.end,则在 i 位置插入 newInterval ,程序结束。否则,将后续所有 intervals[j].start <= newInterval.end (i <= j)的元素合并到 i 位置,intervals[i].start 取涉及合并的最小值, intervals[i].end 取涉及合并的最大值。

3. 将 i 后面没有涉及合并的元素往前移动,移动至紧跟 i 元素。

这道题的思路不难也比较直观,时间效率为 O(n),只是需要处理的边界情况比较多。例如有首部插入区间数组、尾部插入区间数组、以及完全覆盖的区间数组情况。

 vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {

     if (intervals.size() == ) {
intervals.push_back(newInterval);
return intervals;
} if (newInterval.end < intervals[].start) {
intervals.insert(intervals.begin(), newInterval);
return intervals;
} if (newInterval.start > intervals[intervals.size()-].end) {
intervals.push_back(newInterval);
return intervals;
} if (newInterval.start <= intervals[].start && intervals[intervals.size()-].end <= newInterval.end) {
intervals.clear();
intervals.push_back(newInterval);
return intervals;
} int len = (int)intervals.size();
int i = ;
for ( ; i < intervals.size(); i++) {
if (intervals[i].end >= newInterval.start) {
break;
}
}
int theIdx = i; if (intervals[theIdx].start > newInterval.end) {
vector<Interval>::iterator nth = intervals.begin() + theIdx;
intervals.insert(nth, newInterval);
return intervals;
}else{
intervals[theIdx].start = min( intervals[theIdx].start, newInterval.start); } while (i < intervals.size() && intervals[i].start <= newInterval.end) {
intervals[theIdx].end = max(intervals[i].end, newInterval.end);
i++;
} int j = theIdx + ; while (i < intervals.size()) {
intervals[j] = intervals[i];
i++;
j++;
} while (j < len) {
intervals.pop_back();
j++;
} return intervals;
}

[LeetCode] 57. Insert Interval 解决思路的更多相关文章

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

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

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

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

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

  4. [LeetCode] 57. Insert Interval 插入区间

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

  5. LeetCode: 57. Insert Interval(Hard)

    1. 原题链接 https://leetcode.com/problems/insert-interval/description/ 2. 题目要求 该题与上一题的区别在于,插入一个新的interva ...

  6. LeetCode 57. Insert Interval 插入区间 (C++/Java)

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

  7. [leetcode]57. Insert Interval插入区间

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

  8. Leetcode#57 Insert Interval

    原题地址 遍历每个区间intervals[i]: 如果intervals[i]在newInterval的左边,且没有交集,把intervals[i]插入result 如果intervals[i]在ne ...

  9. Leetcode 57: Insert Interval 让代码更好读, 更容易测试.

    阅读了几个博客, 决定写一个简易版本; 忙着做更多题, 没有时间多考虑优化代码, 所以, 就写一个试试运气. http://blog.csdn.net/kenden23/article/details ...

随机推荐

  1. [转] What is Ec/Io (and Eb/No)?

    PS:http://www.telecomhall.com/what-is-ecio-and-ebno.aspx If someone asks you "Which Signal Leve ...

  2. mysql 创建数据库使用默认字符集(备忘)

    GBK: create database test2 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci; UTF8: CREATE DATABASE ` ...

  3. yii中常用路径<转>

    调用YII框架中jquery:Yii::app()->clientScript->registerCoreScript(‘jquery’); framework/web/js/source ...

  4. 让linux(centos)支持中文文件和文件夹

    一.让linux支持中文 1.将Linux的env设置了LANG=en_US.UTF-8: 2.本地的Shell客户端编码也设置成UTF-8,这样让在windows上传到linux的文件或者目录不会出 ...

  5. 看android的书的体会

    android书上面的代码有时候有问题,可以在网上搜索这些功能.网上和官方文档里面有很好的说明和例子.

  6. 重复造轮子感悟 – XLinq性能提升心得

    曾经的两座大山 1.EF 刚接触linq那段时间,感觉这家伙好神奇,语法好优美,好厉害.后来经历了EF一些不如意的地方,就想去弥补,既然想弥补,就必须去了解原理.最开始甚至很长一段时间都搞不懂IQue ...

  7. mysql UNIX时间戳与日期的相互转换

    UNIX时间戳转换为日期用函数: FROM_UNIXTIME() select FROM_UNIXTIME(1156219870); 日期转换为UNIX时间戳用函数: UNIX_TIMESTAMP() ...

  8. web项目环境搭建(3):搭建Spring+MyBatis

    spring 4.1.7 + MyBatis 3.3 正式开始配置关键内容,这是硬货 一. 新建spring配置文件,起名为 applicationContext.xml,放在src/main/res ...

  9. An FPS counter.

    本文由博主(YinaPan)原创,转载请注明出处:http://www.cnblogs.com/YinaPan/p/Unity_FPFCounter.html using UnityEngine; u ...

  10. decode()与case then 学习与使用

    今天做项目的时候遇到一个oracle数值转换的问题,按需求需要对匹配系统时间进行固定赋值,为了避免增加复杂度并易于维护,尽量不要使用存储过程或触发器,最好是使用oracle 自带函数. 如: SQL& ...