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

SOLUTION 1:

1. 先使用Comparator 的匿名类对intervels进行排序。

2. 把Intervals遍历一次,依次一个一个merge到第1个interval。 把第1个interval设置为last(最后一个加到结果集里)

有2种情况:

(1) cur在last的后面。把last加到结果集,将cur加到结果集中。

(2)cur与last有重合部分,把它们合并,合并之后的区间作为新的last.

所有的都扫完后,把last加到结果集中即可。

注意:因为现在Leetcode所有入参都搞成了List,所以遍历时最好使用Iterator,这样无论对于Linkedlist,还是arraylist,性能都是一样的,否则使用get(i)对于LinkedList会相当的缓慢,就不是O(1)的时间了。

复杂度:排序是NlogN, 而merge本身是N。所以总体时间复杂度是NlogN

 /**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public List<Interval> merge(List<Interval> intervals) {
List<Interval> ret = new ArrayList<Interval>();
if (intervals == null || intervals.size() == 0) {
return ret;
} Collections.sort(intervals, new Comparator<Interval>() {
public int compare(Interval o1, Interval o2) {
// sort the intervals by the start.
return o1.start - o2.start;
}
}); // 作为最后一个插入的区间
Interval last = intervals.get(0); // 这里要考虑性能。使用iterator的话,对linkedlist会更快.
Iterator<Interval> itor = intervals.iterator();
while (itor.hasNext()) {
Interval cur = itor.next();
// cur 在last的右边
if (cur.start > last.end) {
// 将cur作为新的last.
ret.add(last);
last = cur;
// cur与last有重合的部分,合并之
} else {
int s = last.start;
int e = Math.max(last.end, cur.end);
last = new Interval(s, e);
}
} // 把最后一个区间加上
ret.add(last); return ret;
}
}

GITHUB CODE

REF: http://blog.csdn.net/fightforyourdream/article/details/16882295

LeetCode: Merge Intervals 解题报告的更多相关文章

  1. 【LeetCode】56. Merge Intervals 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【LeetCode】435. Non-overlapping Intervals 解题报告(Python)

    [LeetCode]435. Non-overlapping Intervals 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...

  3. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  4. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  5. LeetCode - Course Schedule 解题报告

    以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...

  6. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  7. [LeetCode] Merge Intervals 排序sort

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

  8. [LeetCode] 56. Merge Intervals 解题思路

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

  9. 【LeetCode】915. Partition Array into Disjoint Intervals 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/partitio ...

随机推荐

  1. 从官方的BZR源安装avant-window-navigator

    资料来自: http://blog.163.com/azhai@126/blog/static/111056312008315842433/http://www.ibentu.org/2007/09/ ...

  2. 【协议篇】TCP

    TCP 百科名片 TCP:Transmission Control Protocol 传输控制协议TCP是一种面向连接(连接导向)的.可靠的.基于字节流的运输层(Transport layer)通信协 ...

  3. 优化后队列的实现(C语言实现)

    上一篇中的队列的定义与实现(C语言实现) 中.不管是顺序队列还是链式队列,在尾加和删除头部的操作时.总有一个时间复杂度让人不惬意. 比方在顺序队列中,删除头部的操作后,总要将后面全部的结点都向前移动一 ...

  4. Java中初级数值类型的大小, volatile和包装类wrapped type的比较

    Java中的初级数值类型 Java是静态类型语言, 所有的变量必须先声明再使用. 其初级类型一共8种: boolean: 数据只包含1bit信息, 但是占空间为8-bit, 默认值为false byt ...

  5. 【laravel5.4】引入自定义类库+卸载已有的自定义库(以引入钉钉应用为例)composer dumpautoload -o

    本文之前,首先感谢: Azeroth_Yang  传送门:https://blog.csdn.net/zwrj1130/article/details/73467320 强烈建议引入的类 都是含有命名 ...

  6. PHP-二进制文件和文本文件的区别

    一.文本文件和二进制文件的定义 计算机上所有文件存储在存储设备上都是二进制的, 所以文本文件和二进制文件的区别并不是物理上的, 而是逻辑上的!简单来说, 文本文件是基于字符编码的文件, 常见的编码有A ...

  7. HDUOJ---3371Connect the Cities

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  8. 【LeetCode】141. Linked List Cycle (2 solutions)

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...

  9. C# 采用钩子捕获键盘和鼠标事件-验证是否处于无人操作状态

    原文地址:https://www.cnblogs.com/gc2013/p/4036414.html 全局抽象类定义 using System; using System.Collections.Ge ...

  10. PS_Form个性化复杂需求新增Menu并调用Request(案例)

    2014-06-01 Created By BaoXinjian