LeetCode57 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]. (Hard)
分析:
首先可以采用merge interval的方法,先把区间填进去,然后排序,最后再调用merge,复杂度O(NlogN)
代码:
/**
* 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 {
private:
static bool cmp (const Interval& I1, const Interval& I2) {
return I1.start < I2.start;
}
vector<Interval> merge(vector<Interval>& intervals) {
vector<Interval> result;
if (intervals.size() == ) {
return result;
}
sort(intervals.begin(), intervals.end(), cmp);
int left = intervals[].start, right = intervals[].end;
for (int i = ; i < intervals.size(); ++i) {
if (intervals[i].start <= right) {
right = max(right,intervals[i].end);
}
else {
result.push_back(Interval(left,right));
left = intervals[i].start;
right = intervals[i].end;
}
}
result.push_back(Interval(left,right));
return result;
}
public:
vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {
vector<Interval> result;
intervals.push_back(newInterval);
sort(intervals.begin(),intervals.end(),cmp);
result = merge(intervals);
return result;
}
};
当然还有O(N)的算法可以优化。
分三步来做:
第一步,找到左侧不跟newInterval相交的区间添加到结果中;
第二步,找到所有和newInterval相交的区间并找到其左边界和右边界,然后建立新的interval添加到结果中;
第三部,找到右侧不跟newInterval相交的区间添加到结果中。
注意很多细节在里面可能会犯错
代码:
/**
* 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> result;
if (intervals.size() == ) {
result.push_back(newInterval);
return result;
}
int i = ;
while (i < intervals.size() && intervals[i].end < newInterval.start) {
result.push_back(intervals[i++]);
}
int left = ;
if (i == intervals.size()) {
left = newInterval.start;
}
else {
left = min(newInterval.start,intervals[i].start);
}
while (i < intervals.size() && intervals[i].start <= newInterval.end) {
i++;
}
int right = ;
if (i >= ) {
right = max(newInterval.end, intervals[i - ].end);
}
else {
right = newInterval.end;
}
result.push_back(Interval(left,right));
while (i < intervals.size() ) {
result.push_back(intervals[i++]);
}
return result;
}
};
LeetCode57 Insert Interval的更多相关文章
- [Java]LeetCode57 Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [Swift]LeetCode57. 插入区间 | Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- 【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 ...
- 【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],[ ...
随机推荐
- Codeforces Educational Codeforces Round 15 E - Analysis of Pathes in Functional Graph
E. Analysis of Pathes in Functional Graph time limit per test 2 seconds memory limit per test 512 me ...
- SCAU 10893 Spiral
10893 Spiral 时间限制:1000MS 内存限制:65535K 题型: 编程题 语言: 无限制 Description Given an odd number n, we can ar ...
- dom select选单
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- Primitive Objects
[Primitive Objects] Unity can work with 3D models of any shape that can be created with modelling so ...
- *** WARNING L1: UNRESOLVED EXTERNAL SYMBOL
kei编译时提示: *** WARNING L1: UNRESOLVED EXTERNAL SYMBOL *** WARNING L1:reference made to unresolved ext ...
- hdu 5491 The Next (位运算)
http://acm.hdu.edu.cn/showproblem.php?pid=5491 题目大意:给定一个数D,它的二进制数中1的个数为L,求比D大的数的最小值x且x的二进制数中1的个数num满 ...
- MBR所在位置
如果offset的000000000位置如下图所示(主要看红色框框位置是否出现NTFS字样),说明系统文件是NTFS "EB 52"至"55 AA"位置是MBR ...
- Qt Creator无法用“UTF-8”编码解码
在Qt Creator 里打开其他编辑器的代码时有时会提示: 无法用"UTF-8"编码解码 在文件上右键使用NotePad++编辑器打开: 选择->格式-&g ...
- 浅谈iOS IPv6-only 新规
5月份苹果发布新规,对于开发人员只需要做到以下几点就能顺利上线啦! 1.苹果从6月1日起,提供App Store审核的应用必须要兼容面向硬件识别和网络路由的最新互联网协议--IPv6-only标准.也 ...
- 测试rest接口的两个工具使用详解(restclient+soapUI)
http://www.cnblogs.com/lanxuezaipiao/archive/2013/05/31/3110764.html http://www.oschina.net/news/618 ...