题目要求:

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

解题思路:

1)将集合进行排序;

集合的排序建议使用Collections的sort方法实现,需要自己实现Comparator接口;

2)依次进行归并,当后者的start大于当前Interval的end时,将当前Interval加入到集合中,否则将当前Interval和后者Interval进行归并

注意:边界条件的判断

代码:

 /**
* 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) {
//处理特殊情况
if(intervals == null || intervals.size() <2 ){
return intervals;
} //集合排序
ComparatorImpl com = new ComparatorImpl();
Collections.sort(intervals,com); //归并
List<Interval> temp = new ArrayList<Interval>();
Interval val = intervals.get(0);
Interval next;
boolean flag = true;
for(int i=1; i<intervals.size();){
next = intervals.get(i);
flag = true;
if(next.start > val.end){
temp.add(val);
val = next;
flag = false;
}else{
//进行一次归并,i自增一次
val.end = Math.max(val.end,next.end);
i++;
}
} //处理最后一个未归并的Interval
if(flag){
temp.add(val);
} return temp;
}
} //实现排序接口
class ComparatorImpl implements Comparator<Interval>
{
public int compare(Interval o1, Interval o2) {
// TODO Auto-generated method stub
return o1.start-o2.start;
} }

leetcode56. Merge Intervals的更多相关文章

  1. [array] leetcode-56. Merge Intervals - Medium

    leetcode-56. Merge Intervals - Medium descrition Given a collection of intervals, merge all overlapp ...

  2. Leetcode56. Merge Intervals合并区间

    给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: 区间 [1,3] ...

  3. 【leetcode】Merge Intervals

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

  4. 【leetcode】Merge Intervals(hard)

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

  5. 60. Insert Interval && Merge Intervals

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

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

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

  7. [Leetcode][Python]56: Merge Intervals

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...

  8. 【leetcode】 Merge Intervals

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

  9. Insert Interval & Merge Intervals

    Insert Intervals Given a non-overlapping interval list which is sorted by start point. Insert a new ...

随机推荐

  1. Android --RatingBar的使用

    1.效果图

  2. Servlet处理get请求时的中文乱码问题

    我们都知道,使用Servlet处理get请求时,如果get请求的参数中有中文,直接接收会是乱码,这个时候我们使用类似下面的语句来处理乱码: 12345 String name = request.ge ...

  3. PL/SQL查询oracle数据库对象

    dictionary 全部数据字典表的名称和解释,它有一个同义词dict,dict_column 全部数据字典表里字段名称和解释 如果我们想查询跟索引有关的数据字典时,可以用下面这条SQL语句: se ...

  4. JSch - Java实现的SFTP(文件下载详解篇)

    上一篇讲述了使用JSch实现文件上传的功能,这一篇主要讲述一下JSch实现文件下载的功能.并介绍一些SFTP的辅助方法,如cd,ls等. 同样,JSch的文件下载也支持三种传输模式:OVERWRITE ...

  5. The Bus Driver Problem

    题目连接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=90648#problem/G 题意: 给每位司机分配一个白天和晚上的行车路线, ...

  6. The Unsolvable Problem

    The Unsolvable Problem 题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=45783 题意: ...

  7. Objective-C的新特性

    Objective-C的新特性 苹果在今年的 WWDC2012 大会上介绍了大量 Objective-C 的新特性,能够帮助 iOS 程序员更加高效地编写代码.在不久前更新的 Xcode4.4 版本中 ...

  8. [LintCode] Super Ugly Number 超级丑陋数

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  9. ssh问题

    一.基于秘钥认证: ssh-keygen ssh-copy-id -i .ssh/id_rsa.pub 10.10.10.70#在对方authorized_keys 文件中写入自己的id_rsa.pu ...

  10. WPF中ComboBox绑定数据库自动读取产生数据

    前台端 <ComboBox HorizontalAlignment="Name="cmb_SSBM" DisplayMemberPath="NAME&qu ...