题目:

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. ajax 第一个程序

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  2. 两年内从零到每月十亿 PV 的发展来谈 Pinterest 的架构设计(转)

    原文:Scaling Pinterest - From 0 To 10s Of Billions Of Page Views A Month In Two Years 译文:两年内从零到每月十亿 PV ...

  3. Dagger2 scope

    1. 一个没有scope的component是不能依赖于另外一个有scope的component 2.@Singleton不是真正意义的单例,比如下面 @Singleton @Component cl ...

  4. fedora 16安装ByPass四网口网卡遇到的问题

    这个问题困扰了好几天,今天终于在大谷歌的帮助下,在这个网站http://blog.bwysystems.com/bwysystems/?p=16上找到了答案!还是国外的技术论坛强,在百度上搜遍了也没有 ...

  5. GPIO 配置之ODR, BSRR, BRR 详解

    STM32 GPIO 配置之ODR, BSRR, BRR 详解 用stm32 的配置GPIO 来控制LED 显示状态,可用ODR,BSRR,BRR 直接来控制引脚输出状态. ODR寄存器可读可写:既能 ...

  6. linux下find查找命令用法

    Linux下find命令在目录结构中搜索文件,并执行指定的操作.Linux下find命令提供了相当多的查找条件,功能很强大.由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时 ...

  7. emWin(ucGui) MULTIEDIT控件的按键响应处理 worldsing

    目前没有读过ucgui的源代码,通过应用代码测试出在FRAMEWIN的控件焦点顺序是样的: 按资源列表里创建的控件,默认将焦点落在第一个可接收焦点的控件,目前知道不可接收 焦点的控件有TEXT,在FR ...

  8. 转载sublime text3中package Control

    Sublime Text 3 安装Package Control 原来Subl3安装Package Control很麻烦,现在简单的方法来了 一.简单的安装方法 使用Ctrl+`快捷键或者通过View ...

  9. Http下的各种操作类.WebApi系列~通过HttpClient来调用Web Api接口

    1.WebApi系列~通过HttpClient来调用Web Api接口 http://www.cnblogs.com/lori/p/4045413.html HttpClient使用详解(java版本 ...

  10. Delphi / C++ Builder 使用 UDT ( UDP-based Data Transfer ) 4.11

    添加 src/*.cpp 到工程, 修改 Directories and Conditionals, 添加 WIN32 UDT_EXPORTS udt.h 需要 #pragma link " ...