Insert Interval
在已经排好序的区间中,插入一个新的区间,与merge的做法类似
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) {
vector<Interval> res;
int size = intervals.size();
int i = ;
for (i = ; i < size; i++) {
if(newInterval.end < intervals[i].start){
break;
}else if(newInterval.start > intervals[i].end){
res.push_back(intervals[i]);
continue;
}else{
newInterval.start = min(newInterval.start,intervals[i].start);
newInterval.end = max(newInterval.end,intervals[i].end);
}
}
res.push_back(newInterval);
for(int j = i;j<size;j++){
res.push_back(intervals[j]);
}
return res;
}
};
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 使用模拟 ...
- [OJ] Insert Interval
LintCode #30. Insert Interval (Easy) LeetCode #57. Insert Interval (Hard) class Solution { public: v ...
- [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 ...
随机推荐
- [问题2014S08] 解答
[问题2014S08] 解答 (此解答由徐昊宸同学和鹿彭同学提供) 设 \(P_1(\lambda),P_2(\lambda),Q_1(\lambda),Q_2(\lambda)\) 为可逆 \(\l ...
- 通过Mac远程调试iPhone/iPad上的网页(转)
我们知道在 Mac/PC 上的浏览器都有 Web 检查器这类的工具(如最著名的 Firebug)对前端开发进行调试,而在 iPhone/iPad 由于限于屏幕的大小和触摸屏的使用习惯,直接对网页调试非 ...
- winform开发框架之模块维护
前言:模块维护试图解决的问题, 模块加载只用MEF的方式: MEF(Managed Extensibility Framework)是一个用于创建可扩展的轻型应用程序的库. 应用程序开发人员可利用该库 ...
- Android Studio 简单设置
转自:http://ask.android-studio.org/?/article/14 界面设置 默认的 Android Studio 为灰色界面,可以选择使用炫酷的黑色界面.Settings - ...
- HDU-4527 小明系列故事——玩转十滴水 模拟
题意:就是平时玩的十滴水游戏,游戏者拥有一定的水滴,能够滴在某些位置,如果一个点上的体积超过了4就会爆炸,向四周传递一个小水滴.该题就是要求模拟这个过程. 分析:这里有一个问题就是不能够使用递归来处理 ...
- .ipynb文件 与ipython notebook
没有安装ipython notebook 后看见.ipynb文件直接手足无措了 一.安装ipython notebook 使用命令 pip ipython [all] 为所有用户安装 ipython ...
- Ubuntu安装SSH服务
1 SSH服务 Ubuntu默认并没有安装ssh服务,如果通过ssh远程连接到Ubuntu,需要自己手动安装ssh-server(openssh-server). 1.1 检测是否安装SSH服务 出现 ...
- openfire源码修改聊天消息发送内容
/** * $RCSfile: MessageRouter.java,v $ * $Revision: 3007 $ * $Date: 2005-10-31 13:29:25 -0300 (Mon, ...
- 错误overlay id is not a dependency project原因分析
之前按下面配置,一直会报标题中错误,有的同学说改成<overlay><id></id></overlay>就可以了,然而我这里竟然错误依旧. 奇怪的是间 ...
- string.Format格式化
,)//填充,结果为02,003