[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 ...
随机推荐
- ActionBar只显示图标不显示文字
问题:ActionBar菜单项android:showAsAction设置为android:showAsAction="always|withText"或者android:show ...
- ios PromiseKit
简介: 高级开发是高度异步的,PromiseKit收集了一些帮助函数,让我们开发过程中使用的典型异步模式更加令人愉悦. 1.通过pod安装promisekit: 2. promise.h介绍 @imp ...
- IAP (In-App Purchase)中文文档
内容转自:http://yarin.blog.51cto.com/1130898/549141 一.In App Purchase概览 Store Kit代表App和App Store之间进行通信.程 ...
- Implicit conversion from enumeration type 'enum CGImageAlphaInfo' to different enumeration type 'CGB
Implicit conversion from enumeration type 'enum CGImageAlphaInfo' to different enumeration type 'CGB ...
- requirejs下载与配置
写在开始: requirejs有中文版api文档,可以访问http://www.requirejs.cn/home.html 下载requirejs 访问http://www.requirejs.cn ...
- java线程中生产者与消费者的问题
一.概念 生产者与消费者问题是一个金典的多线程协作的问题.生产者负责生产产品,并将产品存放到仓库:消费者从仓库中获取产品并消费.当仓库满时,生产者必须停止生产,直到仓库有位置存放产品:当仓库空时,消费 ...
- 05_Excel操作_02_模拟Web环境的User列表导出
[思路解释] 在正式上到WebProject之前,准备模拟一下WebProject后台的导出流程. 主要都写在ExcelService层,在Excel的Service层,首先要获得UserList,即 ...
- python 访问php程序,实现定时
#!/usr/bin/python #test2.py import sys import urllib2 j = True jj = 1##########用于统计,所以分页, url = 'htt ...
- PHP类中的__get()和__set函数到底有什么用?
当试图获取一个不可达变量时,类会自动调用__get. 同样的,当试图设置一个不可达变量时,类会自动调用__set. 在网站中,这两个并不是什么非用不可的函数. 例如: Class Test { ...
- SSH框架jar神包
SSH JAR包链接(https://zhidao.baidu.com/share/ac8b1389bac84023d7442cad88f3933c.html)