LintCode #30. Insert Interval (Easy)

LeetCode #57. Insert Interval (Hard)

class Solution {
public:
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
vector<Interval> v;
auto it = intervals.begin();
while (it != intervals.end() && it->end < newInterval.start) {
v.push_back(*it);
++it;
}
auto left = it;
while (it != intervals.end() && it->start <= newInterval.end) ++it;
auto right = it;
if (right - left != 0) {
v.push_back(Interval(min(left->start, newInterval.start), max((right - 1)->end, newInterval.end)));
} else {
v.push_back(newInterval);
}
while (it != intervals.end()) {
v.push_back(*it);
++it;
}
return v;
}
};

思路:

  • 第一个while循环跳过所有不会与newInterval相交且小于newInterval的区间.
  • 中间一段计算相交区间.
  • 最后一个while循环跳过所有不会与newInterval相交且大于newInterval的区间.

时间复杂度: O(n)

空间复杂度: O(n)


参照九章算法的JAVA解法改写的C++版.

class Solution {
public:
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
vector<Interval> v;
int pos = 0;
for (auto interval : intervals) {
if (interval.end < newInterval.start) {
v.push_back(interval);
++pos;
} else if (interval.start > newInterval.end) {
v.push_back(interval);
} else {
newInterval.start = min(newInterval.start, interval.start);
newInterval.end = max(newInterval.end, interval.end);
}
}
v.insert(v.begin() + pos, newInterval);
return v;
}
};

这算法简洁在于: 一个循环, 通过两个if滤掉所有不相交的区间; (若有)剩下的区间一定与newInterval相交, 将这些区间合并到newInterval里, 最后插入即可.

唯一欠缺的地方在于插入操作会有额外的时间开销.

[OJ] Insert Interval的更多相关文章

  1. 【leetcode】Insert Interval

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

  2. 60. Insert Interval && Merge Intervals

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

  3. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

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

  4. leetcode Insert Interval 区间插入

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Insert Interval 使用模拟 ...

  5. [Swift]LeetCode57. 插入区间 | Insert Interval

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

  6. 【LeetCode】57. Insert Interval

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

  7. Leetcode: Merge/Insert Interval

    题目 Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[ ...

  8. [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals

    Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...

  9. 间隔问题,合并间隔(merge interval),插入间隔(insert interval)

    Merge Interval: Given a collection of intervals, merge all overlapping intervals. For example,Given  ...

随机推荐

  1. java 用eclipse j2ee写的servlet 程序,WEB-INF下的配置文件web.xml在哪啊?谢谢!

    我用的版本是tomcat7.0,在webcontent\web-inf里只有一个空文件夹lib,写完servlet 类程序,就可以运行了,我想知道自动生成的配置文件在哪里?或者说从哪里能够看出来配置内 ...

  2. 20160328 javaweb Cookie 小练习

    利用cookie实现历史记录浏览: 由于是简单演示,所以直接用javabean 取代数据库了 数据存储类: package com.dzq.dao; import java.util.*; impor ...

  3. Deep Learning 学习随记(三)续 Softmax regression练习

    上一篇讲的Softmax regression,当时时间不够,没把练习做完.这几天学车有点累,又特别想动动手自己写写matlab代码 所以等到了现在,这篇文章就当做上一篇的续吧. 回顾: 上一篇最后给 ...

  4. Object-C属性(Properties)

    前面我们写了caption和photographer的访问方法,你可能也注意到了,那些代码很简单,应该可以写成具有更普遍意义的形式. 属性是Object-C的一个特性,它允许我们自动生成访问器,同时还 ...

  5. 小改动,大作为——C# 4.0中的微小改动

    1.可选参数和命名实参 可选参数和命名实参就如同一对好基友,因为它们经常一起使用. 1.1 可选参数 可选参数重在“可选”,即在调用方法时,该参数可以明确指定实参,也可以不指定.如下代码所示,下面代码 ...

  6. CSS背景属性

    CSS背景属性 1.background-attachment 属性 scroll:默认值.背景图像会随着页面其余部分的滚动而移动. fixed:当页面的其余部分滚动时,背景图像不会移动. inher ...

  7. 初识Angular2

    Angular2是面向未来的科技,要求浏览器支持ES6+,我们现在要尝试的话,需要加一些 垫片来抹平当前浏览器与ES6的差异: angular2-polyfills - 为ES5浏览器提供ES6特性支 ...

  8. 查看编译后的calss文件编译jdk版本

    使用UtralEdit或者sublime text打开编译后的.class文件, 其中cafe babe为magic number(魔数),标识这个文件是java的class文件. 0033转换成10 ...

  9. Bzoj 3831 [Poi2014]Little Bird

    3831: [Poi2014]Little Bird Time Limit: 20 Sec Memory Limit: 128 MB Submit: 310 Solved: 186 [Submit][ ...

  10. IniParse解析类

    说明 iniParse这个类是一个解析ini文件的类,他的功能和Windows下GetPrivateProfileString的功能一样,可以很方便的保存读取配置. 当然他不是只有GetPrivate ...