题目

Given a collection of intervals, merge all overlapping intervals.

For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].

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].

 
思路:
1. 模拟题
2. Insert Interval 是 Merge Interval 的扩展, 将 newInterval 插入到 Intervals 即可转化成 Merge Interval
 
代码
bool sortInterval(const Interval &i1, const Interval &i2) {
return i1.start < i2.start;
}
class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
vector<Interval> newVec;
if(intervals.size() <= 0)
return newVec; sort(intervals.begin(), intervals.end(), sortInterval);
for(int i = 0; i < intervals.size(); i ++) {
Interval nextInt = intervals[i];
if(i == intervals.size()-1) {
newVec.push_back(nextInt);
break;
}
Interval tmp = intervals[i+1];
while(tmp.start <= nextInt.end) {
nextInt.end = max(tmp.end, nextInt.end);
i++;
if(i+1<intervals.size())
tmp = intervals[i+1];
else {
newVec.push_back(nextInt);
return newVec;
}
}
newVec.push_back(nextInt);
}
return newVec;
}
};

  

Leetcode: Merge/Insert Interval的更多相关文章

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

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

  2. leetcode 57 Insert Interval & leetcode 1046 Last Stone Weight & leetcode 1047 Remove All Adjacent Duplicates in String & leetcode 56 Merge Interval

    lc57 Insert Interval 仔细分析题目,发现我们只需要处理那些与插入interval重叠的interval即可,换句话说,那些end早于插入start以及start晚于插入end的in ...

  3. 【leetcode】Insert Interval

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

  4. leetCode 57.Insert Interval (插入区间) 解题思路和方法

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

  5. 第一周 Leetcode 57. Insert Interval (HARD)

    Insert interval  题意简述:给定若干个数轴上的闭区间,保证互不重合且有序,要求插入一个新的区间,并返回新的区间集合,保证有序且互不重合. 只想到了一个线性的解法,所有区间端点,只要被其 ...

  6. Java for LeetCode 057 Insert Interval

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

  7. [LeetCode] 57. Insert Interval 插入区间

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

  8. LeetCode 57. Insert Interval 插入区间 (C++/Java)

    题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...

  9. [Leetcode][JAVA] Insert Interval

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

随机推荐

  1. PLSQL Split分割字符串

    系统自带的split,使用起来方便,但是如果字符串太长,可能会出现异常,这里,我自己写了一个也是该名字,放在自己的包中,引用的时候带包名就好了. --系统自带的函数 /*CURSOR cur_temp ...

  2. 零基础小白怎么用Python做表格?

    用Python操作Excel在工作中还是挺常用的,因为毕竟不懂Excel是一个用户庞大的数据管理软件.本文用Python3!在给大家分享之前呢,小编推荐一下一个挺不错的交流宝地,里面都是一群热爱并在学 ...

  3. Solr调优参考

    http://rdc.taobao.com/team/jm/archives/1753#nav 共整理三部分,第一部分Solr常规处理,第二部分针对性性处理,前者比较通用,后者有局限性.务必根据具体应 ...

  4. 如何用php启动exe程序,并在进程中查看?

    function query_process($service) { /* **查看WINDOWS系统进程列表,并查找指定进程是否存在 */ $tasklist = $_SERVER["WI ...

  5. 使用spring AOP获得session的思路

    由于Spring 的AOP面向切面编程,与Servlet容器没有任何关联,所以想要获得Session会话比较麻烦. 当然Struts2同样不依赖Servlet容器,可以在Spring AOP中可以使用 ...

  6. 四种对象生存期和作用域、static 用法总结

    一.四种对象生存期和作用域 栈对象 隐含调用构造函数(程序中没有显式调用) 堆对象 隐含调用构造函数(程序中没有显式调用),要显式释放 全局对象.静态全局对象 全局对象的构造先于main函数 已初始化 ...

  7. 字符编解码的故事(ASCII,GBK,Unicode,Utf-8区别)

    很久很久以前,有一群人,他们决定用8个可以开合的晶体管来组合成不同的状态,以表示世界上的万物.他们认为8个开关状态作为原子单位很好,于是他们把这称为"字节". 再后来,他们又做了一 ...

  8. Ubuntu 14.10 -- 异次元软件世界

    Ubuntu 14.10 中文桌面版/服务器正式版下载 - 华丽免费易于入门的 Linux 操作系统 [ 系统工具 - Linux // 2014-10-25 ]     一说到 Linux,就不得不 ...

  9. C++ 基类指针和子类指针相互赋值

    首先,给出基类animal和子类fish [cpp] view plaincopy //======================================================== ...

  10. linux 使用FIO测试磁盘iops 方法详解

    FIO是测试IOPS的非常好的工具,用来对硬件进行压力测试和验证,支持13种不同的I/O引擎, 包括:sync,mmap, libaio, posixaio, SG v3, splice, null, ...