[OJ] Insert Interval
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的更多相关文章
- 【leetcode】Insert Interval
		
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
 - 60. Insert Interval  &&  Merge Intervals
		
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
 - 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
		
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
 - leetcode Insert Interval 区间插入
		
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Insert Interval 使用模拟 ...
 - [Swift]LeetCode57. 插入区间 | Insert Interval
		
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
 - 【LeetCode】57. Insert Interval
		
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
 - Leetcode: Merge/Insert Interval
		
题目 Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[ ...
 - [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals
		
Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...
 - 间隔问题,合并间隔(merge interval),插入间隔(insert interval)
		
Merge Interval: Given a collection of intervals, merge all overlapping intervals. For example,Given ...
 
随机推荐
- Oracel 数据库函数
			
-- Oracle 函数 学习 -- 数值函数 ,(四舍五入, 取整,常用计算,三角) -- 1.四舍五入 round(n[,m]) ,省略m :表示 0 ;m>0 ;小数点后m位 ;m< ...
 - WildFly 9.0.2 + SQLServer + BIRT + JNDI 处理BIRT报表发布时数据源的修改
			
一.前提需求说明: 最近在做项目的时候使用jboss WildFly发布BIRT报表,在报表中我是直接添加的JDBC数据源,通过Database URL .用户名和密码连接数据库.后来在部署到正式和测 ...
 - Ajax返回数据类型
			
MVC中,如果从controller返回的不是一个html,而是一个文本,使用AJAX中如何获取? 后台代码: public ActionResult UploadPicture() { return ...
 - rc4加密
			
function RC4(Expression, Password: string): string; var RB : array[0..255] of Integer; X, Y, Z: long ...
 - ios paper for facebook 使用第三方库
			
facebook paper使用的第三方库 Facebook Paper使用的第三方库 第三方库名 简介 链接 ACE code editor https://github.com/ajaxorg/a ...
 - MyBatis3.1 学习教程
			
昨天中午,突然有想要学习 MyBatis 的冲动,经过 1.5 天的研究和学习,再加上以前学过 I batis 的经验,很快就了解了这门技术. 写这篇教程,是想告诉那些想学却又怕学习不好的同学们, 其 ...
 - c++ primer复习(四)
			
1 标准库容器 顺序容器:vector.list.deque 容器适配器:stack.queue.priority_queue 2 容器元素类型约束: 容器元素类型必须支持复制和赋值,因为容器存放的都 ...
 - EF支持复杂类型的实现
			
本节,将介绍如何手动构造复杂类型(ComplexType)以及复杂类型的简单操作.通常,复杂类型是指那些由几个简单的类型组合而成的类型.比如:一张Customer表,其中有FristName和Last ...
 - Android学习7--日志信息的使用
			
在代码的调试中,日志信息是必不可少的,关于日志信息的使用,接下来,小编简要的说明一下: 日志分为几种:Verbose:从字面上解释,verbose为冗长的,啰嗦的,所以verbose日志信息输出的内容 ...
 - 用 Python写 daemon
			
转自 http://chowroc.blogspot.com/2007/05/python-how-to-write-daemon.html 最近用 Python 可能要写 daemon,找资料先看看 ...