题目:

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

题解:

这道题主要难点是改写Comparator。

Comparator接口定义了两个方法:compare( )和equals( )。这里给出的compare( )方法按顺序比较了两个元素:
  
  int compare(Object obj1, Object obj2)
  
 
 obj1和obj2是被比较的两个对象。当两个对象相等时,该方法返回0;当obj1大于obj2时,返回一个正值;否则,返回一个负值。如果用于比较
的对象的类型不兼容的话,该方法引发一个ClassCastException异常。通过覆盖compare(
),可以改变对象排序的方式。例如,通过创建一个颠倒比较输出的比较函数,可以实现按逆向排序。
  
  这里给出的equals( )方法,测试一个对象是否与调用比较函数相等:
  
  boolean equals(Object obj)
  
  obj是被用来进行相等测试的对象。如果obj和调用对象都是Comparator的对象并且使用相同的排序。该方法返回true.否则返回false.重载equals( )方法是没有必要的,大多数简单的比较函数都不这样做。

具体代码如下:

 1     public ArrayList<Interval> merge(ArrayList<Interval> intervals) {
 2         if (intervals == null || intervals.size() <= 1)
 3             return intervals;
 4  
 5         // sort intervals by using self-defined Comparator
 6         Collections.sort(intervals, new IntervalComparator());
 7  
 8         ArrayList<Interval> result = new ArrayList<Interval>();
 9  
         Interval prev = intervals.get(0);
         for (int i = 1; i < intervals.size(); i++) {
             Interval curr = intervals.get(i);
  
             if (prev.end >= curr.start) {
                 // merged case
                 Interval merged = new Interval(prev.start, Math.max(prev.end, curr.end));
                 prev = merged;
             } else {
                 result.add(prev);
                 prev = curr;
             }
         }
  
         result.add(prev);
  
         return result;
         }
     }
  
     class IntervalComparator implements Comparator<Interval> {
         public int compare(Interval i1, Interval i2) {
             return i1.start - i2.start;
         }

Refrence:

http://www.programcreek.com/2012/12/leetcode-merge-intervals/

http://www.blogjava.net/yesjoy/articles/126046.html

Merge Interval leetcode java的更多相关文章

  1. Insert Interval leetcode java

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

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

    目录 题目链接 注意点 解法 小结 题目链接 Merge Intervals - LeetCode 注意点 区间是无序的 每个区间start一定小于end 解法 解法一:首先以start的值从小到大来 ...

  4. 56. Merge Interval

    56. Merge Interval 0. 参考文献 序号 文献 1 花花酱 LeetCode 56. Merge Intervals 2 [LeetCode] Merge Intervals 合并区 ...

  5. N-Queens II leetcode java

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  6. LintCode 156: Merge Interval

    LintCode 156: Merge Interval 题目描述 给出若干闭合区间,合并所有重叠的部分. 样例 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], ...

  7. 间隔问题,合并间隔(merge interval),插入间隔(insert interval)

    Merge Interval: Given a collection of intervals, merge all overlapping intervals. For example,Given  ...

  8. [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals

    Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...

  9. [Leetcode][JAVA] Insert Interval

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

随机推荐

  1. jupyter notebook变量高亮

    首先声明,anaconda安装的时候,一定要勾选“Add Anaconda to my PATH environment variable”! 否则会有一堆麻烦的问题,做了这一步就能自动添加好路径!不 ...

  2. iOS Sprite Kit教程之滚动场景

    iOS Sprite Kit教程之滚动场景 滚动场景 在很多的游戏中,场景都不是静止的,而是滚动的,如在植物大战僵尸的游戏中,它的场景如图2.26所示. 图2.26  植物大战僵尸 在图2.26中,用 ...

  3. OpenStack计费项目Cloudkitty系列详解(一)

    云计算是一种按需付费的服务模式,虽然OpenStack前期在计量方面走了些“弯路”,但现在的ceilometer.gnocchi.aodh.panko项目的稳步并进算是让其峰回路转.然而,目前来看Op ...

  4. python lambda简单介绍

    python lambda 在python中,如果想要创建函数需要使用关键字def,而如果想要创建匿名函数,就需要使用lambda. lambda创建的函数和def创建的函数有什么区别? def创建的 ...

  5. [ 原创 ]学习笔记-Android 中关于Cursor类的介绍

    此博文转载自:http://www.cnblogs.com/TerryBlog/archive/2010/07/05/1771459.html 主讲Cursor的用法 使用过 SQLite 数据库的童 ...

  6. sublime3176注册码破解汉化及常用插件

    官方网站下载地址:https://www.sublimetext.com/3 破解软件下载地址:https://www.lanzous.com/i1a7zfi 破解软件下载地址备用:https://d ...

  7. [UOJ422]小Z的礼物

    设要取的物品集合为$S$,$E=n(m-1)+(n-1)m$,$x_T$为覆盖了$T$中至少一个元素的$1\times2$数量 $$\begin{aligned}\sum\limits_{i=1}^\ ...

  8. 转MySQL常见错误分析与解决方法总结

    一.Can't connect to MySQL server on 'localhost' (10061)翻译:不能连接到 localhost 上的mysql分析:这说明“localhost”计算机 ...

  9. bzoj 2178

    这题调精度真痛苦啊(向管理员要了数据才调出来). 用的是hwd在WC2015上讲的方法,考虑将原图分割,根据每个圆的左右边界和圆与圆交点的横坐标来分割,这样原图就被分成很多竖着的长条,并且每一条中间都 ...

  10. hdoj 4272 LianLianKan 数据太水

    LianLianKan Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...