题目:

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. 解决SQL Server Always 日志增大的问题-摘自网络

    配置了Alwayson之后,因为没有只能使用完全恢复模式,不能使用简单或大容量日志模式,所以日志不断增长,不能使用改变恢复模式的方式清空日志 手动操作收缩或截断日志也无效 读了一些文章后发现,有人使用 ...

  2. windows下揪出java程序占用cpu很高的线程 并找到问题代码 死循环线程代码

    我的一个java程序偶尔会出现cpu占用很高的情况 一直不知道什么原因 今天终于抽时间解决了 系统是win2003 jvisualvm 和 jconsole貌似都只能看到总共占用的cpu 看不到每个线 ...

  3. 应用c#读取带cookie的http数据

    @(编程) private static string Login() { string url = string.Format("{0}/login-submit.html?identit ...

  4. JavaScript面向对象简介

    JavaScript面向对象简介 @(编程) [TOC] 1. 命名空间 命名空间是一个容器,它允许开发人员在一个独特的,特定于应用程序的名称下捆绑所有的功能. 在JavaScript中,命名空间只是 ...

  5. Java和MongoDB之Hello World

    1.新建Project 新建Java Project,并把mongo-java-driver驱动加入到项目bulid path中,如果你使用的是maven增加依赖. <dependency> ...

  6. sql server对并发的处理-乐观锁和悲观锁【粘】

    假如两个线程同时修改数据库同一条记录,就会导致后一条记录覆盖前一条,从而引发一些问题. 例如: 一个售票系统有一个余票数,客户端每调用一次出票方法,余票数就减一. 情景: 总共300张票,假设两个售票 ...

  7. 后台动态设置前台标签内容和属性(转自http://www.wzsky.net/html/Program/net/26171.html)

    和以前的asp不同,在asp.net中为了彻底的代码分离,我们一般不采用<%=%>嵌入标签中来设置一些属性和内容.一般来说有2种情况:(一)设置标签的内容,比如<title>这 ...

  8. OC三种方法实现定时器

    在软件开发过程中,我们常常需要在某个时间后执行某个方法,或者是按照某个周期一直执行某个方法.在这个时候,我们就需要用到定时器. 在iOS中有很多方法完成定时器的任务,例如 NSTimer.CADisp ...

  9. 负载均衡SESSION同步总结

    1.redis/分布式文件存储系统/数据库 存储session,解决负载均衡集群中session不一致问题 http://www.cnblogs.com/painsOnline/p/5194851.h ...

  10. C# —— IList, ArrayList与List的区别详解

    共同点:    IList, List , ArrayList 通俗一点来讲就是广义的数组,C#里面称之为集合.不同于一般的狭义的数组,它们可以存放任意类型的东西,在申明或者赋值的时候指定. 比如你写 ...