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. android 程序防止被360或者系统给kill掉

    关于如果和防止android 程序防止被360kill掉之后重启的问题,肯定大家也搜索了好多方法,都不好使,对不对,什么增高权限了,什么进程优先级了,这些东西都不是我们可控的,所以有没有一些非常保险的 ...

  2. IntelliJ IDEA 中module的dependencies是其它module时的注意事项

    Dependencies on other modules If a module (module A) depends on another module (module B), IntelliJ ...

  3. Java基础知识强化26:Object类之hashCode()方法、getClass()方法

    1. Object类的hashCode()方法,如下: public  int  hashCode():返回该对象的哈希码值,这个值和地址值有关,但是不是实际地址值(哈希码值是根据实际地址值转化过来的 ...

  4. 大数据笔记01:大数据之Hadoop简介

    1. 背景 随着大数据时代来临,人们发现数据越来越多.但是如何对大数据进行存储与分析呢?   单机PC存储和分析数据存在很多瓶颈,包括存储容量.读写速率.计算效率等等,这些单机PC无法满足要求. 2. ...

  5. Python基础----函数

    1.作用域: 对于变量的作用域,执行声明并在内存中存在,该变量就可以在下面的代码中使用. if 1==1: name = 'wupeiqi' print name 下面的结论对吗? 外层变量,可以被内 ...

  6. mui实现支付宝支付功能

    <!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>H ...

  7. noip 2012 开车旅行

    /*考场上写的暴力 40分钟70分*/ #include<iostream> #include<cstdio> #include<cstring> #define ...

  8. XML数据的读取—数据库配置文件

    数据库配置文件(config.xml) <?xml version="1.0" encoding="utf-8"?> <configurati ...

  9. 使用三层交换机的ACL实现不同vlan间的隔离

    使用三层交换机的ACL实现不同vlan间的隔离   建立三个vlan vlan10 vlan20 vlan30    www.2cto.com   PC1 PC3属于vlan10 PC2 PC4属于v ...

  10. 002_系统表查询(sysdatabases等)

    002_系统表查询(sysdatabases等) --1.获取所有数据库名: SELECT Name FROM Master..SysDatabases ORDER BY Name --2.获取所有表 ...