题目:

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. php操作mongodb or查询这样写!

    $where['$or'] = [ ['id' => ['lt'=>0]], ['id2' => ['lt'=>1]] ]; 这个是查询 id>0 或者id2>1的 ...

  2. PAGELATCH_EX Contention on 2:1:103

    This blog post is meant to help people troubleshoot page latch contention on 2:1:103. If that’s what ...

  3. 减少TIME_WAIT连接状态

    减少TIME_WAIT连接状态.网络上已经有不少相关的介绍,大多是建议: shell> sysctl net.ipv4.tcp_tw_reuse=1 shell> sysctl net.i ...

  4. django导出excel

    # coding:utf-8 from django.http import HttpResponse from xlwt import * import StringIO, os from test ...

  5. JS Function Arguments

    Function arguments在ECMAScript中的行为并不像其他大多数语言中的函数参数. 在ECMAScript中,function 并不关心有多少个参数传入函数中,也不关心传入参数的数据 ...

  6. 1316 文化之旅 2012年NOIP全国联赛普及组

      题目描述 Description 有一位使者要游历各国,他每到一个国家,都能学到一种文化,但他不愿意学习任何一种文化超过一次(即如果他学习了某种文化,则他就不能到达其他有这种文化的国家).不同的国 ...

  7. 解决同伴收获&解决同伴问题补分博客

    解决同伴问题 要求: 查看同组同学的课堂笔记,尝试解决同伴的问题,格式如下: 我的同组同学是XXXX学号XXXX同学 同组同学的问题是XXXX 我理解他的意思是XXXX 他的问题我有一个小建议是XXX ...

  8. TensorFlow安装和HelloWorld

    TensorFlow安装 TensorFlow可以在各种操作系统上面安装.安装的时候要注意TensorFlow的类型,一种是普通的版本,仅支持CPU,安装简单.另外一种类型带GPU的,可以利用GPU来 ...

  9. gridview DataFormatString 属性设置须知

    设置 DataFormatString 进行格式化数据时默认情况下是不会起作用的还有设置HtmlEncode = "false" 具体为什么?以下几点1. 在GridView中的a ...

  10. Spring源码解析 – @Configuration配置类及注解Bean的解析

    在分析Spring 容器创建过程时,我们知道容器默认会加载一些后置处理器PostPRocessor,以AnnotationConfigApplicationContext为例,在构造函数中初始化rea ...