[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 ...
随机推荐
- 用 C# 如何判断数据库中是否存在一个值
选定一个列,比如用户编号列 //欲插入的用户编号string ll_userID="xxxxxxxx"; //查询此编号是否存在SqlCommand mycmd = new Sql ...
- C语言study一
之前看了CPP,敲过些代码,但总觉得学得不够系统,书后面的习题比较无聊,不想去写.C主要在学数据结构和理解底层上面很有帮助. 于是又来mooc学习C语言,看了下视频虽然很繁琐啰嗦,但是有些东西我确实以 ...
- 二维码QRCode
package com.aig.ecompass.ecard; import java.awt.image.BufferedImage; import java.io.File; import jav ...
- 基于注解整合struts2与spring的时候如果不引入struts2-spring-plugin包自动装配无效
基于注解整合struts2与spring的时候如果不引入struts2-spring-plugin包,自动装配将无效,需要spring注入的对象使用时将抛出空指针异常(NullPointerExcep ...
- linux命令行执行db2存储过程
存储过程代码如下: CREATE PROCEDURE proc_sum2(IN n INT,OUT sum INT,OUT j INT) BEGIN DECLARE i INT; ; ; ; WHIL ...
- web.xml配置中的 文件类型<mime-mapping>
<mime-mapping> <extension>doc</extension> <mime-type>application/msword</ ...
- webui layout like desktop rich client
similarity similarlike desktop js frameworklike extj js frameworklike rich client js frameworkjs lay ...
- HDU 3920 Clear All of Them I(DP + 状态压缩 + 贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3920 题目大意:你在一个位置用激光枪灭敌人,给你初始位置,下面是2*n个敌人的位置,你一枪能杀两个,可 ...
- isset(), empty()
isset()测试$a = '';isset($a); // true $a = FALSE;var_dump(isset($a)); // true $a = NULL;var_dump(isset ...
- Java调用.Net WebService参数为空解决办法 (远程)调试webservice方法 转
Java调用.Net WebService参数为空解决办法 (远程)调试webservice方法 同事遇到一个很囧的问题,java调,netwebservice的时候,调用无参数方法成功,调用有参 ...