LeetCode_Insert Interval
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的更多相关文章
- 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 * ...
- [LeetCode] Find Right Interval 找右区间
Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...
- [LeetCode] Insert Interval 插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- angularjs 中的setTimeout(),setInterval() / $interval 和 $timeout
$interval window.setInterval的Angular包装形式.Fn是每次延迟时间后被执行的函数. 间隔函数的返回值是一个承诺.这个承诺将在每个间隔刻度被通知,并且到达规定迭代次数后 ...
- MySQL interval()函数
INTERVAL(N,N1,N2,N3,..........) INTERVAL()函数进行比较列表(N,N1,N2,N3等等)中的N值.该函数如果N<N1返回0,如果N<N2返回1,如果 ...
- oracle11g interval(numtoyminterval())自动创建表分区
Oracle11g通过间隔分区实现按月创建表分区 在项目数据库设计过程中由于单表的数据量非常庞大,需要对表进行分区处理.由于表中的数据是历史交易,故按月分区,提升查询和管理. 由于之前对于表分区了解不 ...
- 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 ...
- Leetcode Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- Hdu 5489 合肥网络赛 1009 Removed Interval
跳跃式LIS(nlogn),在普通的转移基础上增加一种可以跨越一段距离的转移,用一颗新的树状数组维护,同时,我们还要维护跨越完一次后面的转移,所以我用了3颗树状数组.. 比赛的时候一句话位置写错了,然 ...
随机推荐
- Borland license information was found,but it is not valid for delphi.
The start Delphi7 come amiss: Borland license information was found,but it is not valid for delphi. ...
- CMAKE 生成VS2008静态库工程 与 CMAKE使用,CMakeLists.txt编写总结
cmake -G"Visual Studio 9 2008 Win64" 以上命令得用cd命令切换到顶层CMakeLists.txt的当前目录,才能生效 以下是CMakeLists ...
- bzoj1143
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1143 首先用传递闭包,知道一个点是否可以到达另一个点,即mp[i][j]==1表示i可以到j: ...
- 移动互联与O2O的完美衔接
移动互联网虽然市场颇大,前景广阔,但是由于数据过于密集,很难精准的定位所谓的目标客户群,然而O2O的线下市场却与互联网市场有极大的反差.一直觉得高校周边的小商家是最幸福的生意人,客户明确(就是本校学生 ...
- xm学习笔记
1关于静态网页的制作 html主要负责页面的结构+css页面的美观+js与用户的交互. 2html 有标签体的标签: <p></p> <span></spa ...
- poj 2100 Graveyard Design(尺取法)
Description King George has recently decided that he would like to have a new design for the royal g ...
- hdu 5396 Expression(区间dp)
Problem Description Teacher Mai has n numbers a1,a2,⋯,anand n−1 operators("+", "-&quo ...
- 最全的js正则表达式用法大全
匹配中文字符的正则表达式: [u4e00-u9fa5] 评注:匹配中文还真是个头疼的事,有了这个表达式就好办了 匹配双字节字符(包括汉字在内):[^x00-xff] 评注:可以用来计算字符串的长度(一 ...
- hdu 3917 最大重量封闭图
/*最大重量封闭图: 意甲冠军:一些城市要建路需要负责一些公司,每家公司都需要缴纳个税.该公司将需要花费每路,另一个限制条件,如果那家公司a既定a-b.公司b既定b-c然后选择 公司a 你必须选择一个 ...
- 照猫画虎学gnuplot之简单介绍
简单介绍:Gnuplot是一个命令行驱动的科学画图工具,可将数学函数或数值资料以平面图或立体图的形式画在不同种类终端机或画图输出装置上. 它是由Colin Kelley 和 Thomas Willia ...