Question:

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:

public class Solution {
class IntervalAsc implements Comparator<Interval>
{
public int compare (Interval o1, Interval o2)
{
if (o1.start != o2.start)
return o1.start < o2.start ? -1 : 1;
else if (o1.end != o2.end)
return o1.end < o2.end ? -1 : 1;
else
return 0;
}} public ArrayList<Interval> merge (ArrayList<Interval> intervals)
{
Collections.sort(intervals, new IntervalAsc());
ArrayList<Interval> ret = new ArrayList<Interval>();
int n = intervals.size();
if(n==0) return ret;
Interval last=intervals.get(0);
for(int i=1;i<n;i++){
if(intervals.get(i).start>last.end){
ret.add(new Interval(last.start,last.end));
last=intervals.get(i);
}else{
last.end=Math.max(intervals.get(i).end, last.end);
}
}
ret.add(last);
return ret;
} }

需要注意的以下几点:

  1. 这里需要new一个Interval,再放进ArrayList result里。刚开始我就是直接把last放进了result里,忘记了last只是一个类似于指针一样的东西。
  2. 第一次使用到了内部类。更多的内部类的知识看下一篇博客。
  3. Collections.sort的用法。(compare函数的重写)

[Leetcode] Merge Intevals的更多相关文章

  1. [LeetCode] Merge Sorted Array 混合插入有序数组

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...

  2. [LeetCode] Merge Intervals 合并区间

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

  3. [LeetCode] Merge k Sorted Lists 合并k个有序链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...

  4. [LeetCode] Merge Two Sorted Lists 混合插入有序链表

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  5. [Leetcode] Merge Sorted Array (C++)

    我在Github上新建了一个解答Leetcode问题的Project, 大家可以参考, 目前是Java 为主,里面有leetcode上的题目,解答,还有一些基本的单元测试,方便大家起步. 题目: Gi ...

  6. LeetCode Merge k Sorted Lists 解决报告

    https://oj.leetcode.com/problems/merge-k-sorted-lists/ 归并K已经整理阵列,和分析算法的复杂. 解决报告:无论是不考虑优化,最简单的实现是要重新走 ...

  7. [LeetCode] Merge Two Binary Trees 合并二叉树

    Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...

  8. [leetcode]Merge Intervals @ Python

    原题地址:https://oj.leetcode.com/problems/merge-intervals/ 题意: Given a collection of intervals, merge al ...

  9. [leetcode]Merge Sorted Array @ Python

    原题地址:https://oj.leetcode.com/problems/merge-sorted-array/ 题意:Given two sorted integer arrays A and B ...

随机推荐

  1. Oracle Redo Log

    http://blog.itpub.net/27039319/viewspace-2120623/ 11.2和11.2以下的区别:http://blog.itpub.net/27039319/view ...

  2. Linux常用的日志分析命令与工具

    >>基础命令 操作 命令 说明 查看文件的内容 cat -n access.log -n显示行号 分页显示文件 more access.log Enter下一行,空格下一页,F下一屏,B上 ...

  3. Hadoop 苦旅(1)——准备以及Cygwin安装

    安装篇: 安装是最基本的,也是最难的.俗话说的好,万事开头难啊!的确如此.刚开始,自己折腾,总会是这样那样的问题,也许一个小的问题,就要推倒了重来.我现在就将这几天(2014-2-16~2014-2- ...

  4. 【openGL】四面体

  5. 6-05使用SQL语句删除数据

    删除数据语法: DELETE  FROM 表名  WHERE  删除条件. TRUNCATE  TABLE 表名. --[1]基本删除,省略WHERE条件,将删除表中的所有数据 DELETE FROM ...

  6. ASP.NET的新成员ASP.NET WebHooks

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:前几天微软除了发布了ASP.NET 5的Beta7之外,还有一个值得关注的东西,就是A ...

  7. Java 体系结构

    Java体系结构包括四个独立但相关的技术: 当编写并运行一个Java程序时,就同时体验了这四种技术.运行流程如下: Java虚拟机的主要任务是装载class文件并且执行其中的字节码.Java虚拟机包含 ...

  8. 配置ogg异构oracle-mysql 双向同步注意事项

    双向同步需要考虑的是怎么解决循环复制,以及同时更新一张表以谁为基准. 配置过程就不写了,大致和oracle到mysql的单向+mysql到oracle的单向差不多. 需要注意的有如下几点: 1.ora ...

  9. ListView遍历每个Item出现NullPointerException的异常(转)

    在使用ListView过程中我们有时候需要遍历取得每个Item项中的一些数据(比如每个Item里面有TextView,需要获取它的文本等等),但是我们在遍历过程中经常会遇到NullPointerExc ...

  10. Arduino101学习笔记(五)—— 模拟IO

    1.配置IO管脚 //***************************************************************************************** ...