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. G方法的华丽升级

    ThinkPHP长期以来需要通过debug_start.debug_end方法甚至Debug类才能完成的功能,3.1版本中被一个简单的G方法取代了,不可不谓是一次华丽升级.G方法的作用包括标记位置和区 ...

  2. javascript-03

    1.Object   |-1.var  变量=new Object();         |-变量.自定义的属性='值';     |-变量.自定义名称=function(){}   |-2.var ...

  3. mysql查询练习

    mysql> #查询每个栏目最贵的商品 mysql> select goods_id,shop_price,cat_id from (select goods_id,shop_price, ...

  4. js知识

    行为=事件+由该事件触发的动作 常用事件:onXxx onLoad           打开网页事件 onUnLoad       关闭事件 onClick        单击事件 onChange  ...

  5. 封装一个ISortable接口

    using System;/// <summary>/// 排序规范/// </summary>/// <typeparam name="T"> ...

  6. ios paper for facebook 使用第三方库

    facebook paper使用的第三方库 Facebook Paper使用的第三方库 第三方库名 简介 链接 ACE code editor https://github.com/ajaxorg/a ...

  7. 【转帖】客户端通过 HTTP 请求和响应 的 Header 信息总结

    请求Header原帖地址:http://technique-digest.iteye.com/blog/1174581 响应Header原帖地址:http://blog.pfan.cn/hurongl ...

  8. ios专题 -block用法

    what is block Blocks are a language-level feature added to C, Objective-C and C++, which allow you t ...

  9. java ,js获取web工程路径

    一.java获取web工程路径 1),在servlet可以用一下方法取得: request.getRealPath(“/”) 例如:filepach = request.getRealPath(“/” ...

  10. == 和 equals()的区别

    package com.liaojianya.chapter1; /** * This program demonstrates the difference between == and equal ...