题目:

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的更多相关文章

  1. [Java]LeetCode57 Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  2. [Swift]LeetCode57. 插入区间 | Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  3. 【leetcode】Insert Interval

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  4. 60. Insert Interval && Merge Intervals

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  5. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  6. leetcode Insert Interval 区间插入

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Insert Interval 使用模拟 ...

  7. [OJ] Insert Interval

    LintCode #30. Insert Interval (Easy) LeetCode #57. Insert Interval (Hard) class Solution { public: v ...

  8. 【LeetCode】57. Insert Interval

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  9. Leetcode: Merge/Insert Interval

    题目 Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[ ...

随机推荐

  1. MySQL/MariaDB/Percona数据库升级脚本

    MySQL/MariaDB/Percona数据库升级脚本截取<OneinStack>中upgrade_db.sh, 一般情况下不建议升级数据库版本,该脚本专提供给各位版本控们.为防止大版本 ...

  2. 【OpenOffice+swftools】在线预览环境的搭建和xpdf中文包的配置

    [环境参数] Host:Win7 64bit VMware:VMware Workstation11.1.0 Client OS:CentOS release 6.5 (Final) 2.6.32-4 ...

  3. 内存中的static、const实现形式

    最近在考虑下半年找工作的事情,看了不少面试题目,其中还是蛮有收获的,把基础好好复习了一遍.比如这个题目,static.const现形式,static和const类型的变量在写程序的时候也写了很多,不过 ...

  4. C++11常量表达式

    [C++11之常量表达式] 关键字:constexpr: 中文学名:常量表达式. constexpr用于把运行期计算放置在编译期. 使用constexpr有3个限制: 1.函数中只能有一个return ...

  5. POJ 1410 Intersection(判断线段交和点在矩形内)

    Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9996   Accepted: 2632 Desc ...

  6. Android_UI_点击按钮切换背景效果实现

    实现按钮按下和释放,按钮背景图片相应切换效果的方法这里介绍两种,一种是在代码里实现,另一种是在xml文件里实现 一.在xml文件里 首先现在layout的一个xml文件下定义Button如下所示: [ ...

  7. C# JSON使用的常用技巧(一)

    获取JSON无格式的字符串: new JArray().ToString(Newtonsoft.Json.Formatting.None): 如: {"A":123,"B ...

  8. ajax 本地测试,使用Chrome 浏览器

    出现问题: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is ...

  9. [Mac]Mac中显示资源库文件夹

    在 Mac OS X 10.7 Lion 之后的版本中 , 用户的个人目录内的资源库文件默认是隐藏状态. 这个设定可能是为了避免用户误操作. 但是对于中高级用户来说会有些不变. 通过如下方式可以找回被 ...

  10. J2EE项目相对路径、绝对路径获取

    String path = getServletContext().getRealPath("/"); 这将获取web项目的全路径. this.getClass().getClas ...