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

  

/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class Solution {
public:
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<Interval> result; int i = ;
int n = intervals.size(); while( i < n && newInterval.start > intervals[i].end )
result.push_back(intervals[i++]); while(i < n && newInterval.end >= intervals[i].start)
{
newInterval.start = newInterval.start < intervals[i].start ? newInterval.start :
intervals[i].start; newInterval.end = newInterval.end > intervals[i].end ? newInterval.end :
intervals[i].end; i++; } result.push_back(newInterval); while(i< n)
result.push_back(intervals[i++]) ; return result ;
}
};

LeetCode_Insert Interval的更多相关文章

  1. Failure to find xxx in xxx was cached in the local repository, resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced @ xxx

    问题: 在linux服务器上使用maven编译war时报错: 16:41:35 [FATAL] Non-resolvable parent POM for ***: Failure to find * ...

  2. [LeetCode] Find Right Interval 找右区间

    Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...

  3. [LeetCode] Insert Interval 插入区间

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

  4. angularjs 中的setTimeout(),setInterval() / $interval 和 $timeout

    $interval window.setInterval的Angular包装形式.Fn是每次延迟时间后被执行的函数. 间隔函数的返回值是一个承诺.这个承诺将在每个间隔刻度被通知,并且到达规定迭代次数后 ...

  5. MySQL interval()函数

    INTERVAL(N,N1,N2,N3,..........) INTERVAL()函数进行比较列表(N,N1,N2,N3等等)中的N值.该函数如果N<N1返回0,如果N<N2返回1,如果 ...

  6. oracle11g interval(numtoyminterval())自动创建表分区

    Oracle11g通过间隔分区实现按月创建表分区 在项目数据库设计过程中由于单表的数据量非常庞大,需要对表进行分区处理.由于表中的数据是历史交易,故按月分区,提升查询和管理. 由于之前对于表分区了解不 ...

  7. maven执行报错resolution will not be reattempted until the update interval of nexus h

    maven在执行过程中抛错: 引用 ... was cached in the local repository, resolution will not be reattempted until t ...

  8. Leetcode Insert Interval

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

  9. Hdu 5489 合肥网络赛 1009 Removed Interval

    跳跃式LIS(nlogn),在普通的转移基础上增加一种可以跨越一段距离的转移,用一颗新的树状数组维护,同时,我们还要维护跨越完一次后面的转移,所以我用了3颗树状数组.. 比赛的时候一句话位置写错了,然 ...

随机推荐

  1. HDU 4116 Fruit Ninja

    http://acm.hdu.edu.cn/showproblem.php?pid=4116 题意:给N个圆,求一条直线最多能经过几个圆?(相切也算) 思路:枚举中心圆,将其他圆的切线按照极角排序,并 ...

  2. FB是磁珠的符号

    本文来自:http://www.coofish.org/post/FB-cizhu.html今天实验室一哥们研究DSP电路图,发现图中有一个符号是FB,外形有点像电阻(R),但是不清楚是什么电子元器件 ...

  3. 关于selenium中的sendKeys()隔几秒发送一个字符

    看一下你的IEDriverServer.exe是不是64位的,我也遇到了这样的问题,换成32位的IEDriverServer.exe,瞬间速度快了

  4. 合天网安实验室学习笔记----Linux基础

    一.Linux的发展 linux kernel的版本是:2.6.32-279.el6.x86_64,此格式为:主版本.次版本.释出版本-修改版本 次版本为奇数:测试版: 次版本为偶数:稳定版: Lin ...

  5. iOS7 UI适配教程

    最近写了点iOS7适配的文章,请指正 ios6to7 1 ios6to7 2

  6. phpcms:一、安装及新建模板

    1.复制D:\WWW\phpcms\phpcms\templates\目录下的default文件粘贴在当前目录下,并重命名为新模板名字(youpinzhiyuan2012) 2.打开D:\WWW\ph ...

  7. springmvc+mongodb+maven 项目搭建配置

    操作步骤我就不再细化了 项目能运行,测试过了,先上配置,另一篇文章上代码,点击下载源码 项目结构 pom.xml <project xmlns="http://maven.apache ...

  8. 在ubuntu14.04上安装maven

    1.首先到Maven官网下载安装文件,目前最新版本为3.3.1,下载文件为apache-maven-3.0.3-bin.tar.gz,下载可以使用wget命令: 2.进入下载文件夹,找到下载的文件,运 ...

  9. android 点击桌面图标,打开手机浏览器进入对应的站点

    做一个假的adnroid app.要实现点击桌面图标.打开手机浏览器进入对应的站点,实现方法非常easy import android.app.Activity; import android.con ...

  10. Ubuntu 10.04下安装Opengl glx

    1.安装OpenGL Library sudo apt-get install build-essential 2. 安装OpenGL Utilities sudo apt-get install l ...