题目要求:

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. js从身份证号中获取出生日期和性别

    今天,在做移动端的项目中,按照设计稿的要求,是可以让用户自己输入出生日期的,我还很认真的用了刚刚知道的html5表单的日期类型,本想着终于不用日期插件就可以实现用户选择自己的出生日期了,可结果老大说, ...

  2. [LintCode] Reverse Integer 翻转整数

    Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...

  3. MySQL Command 常见命令

    /* Load data from txt file */ LOAD DATA LOCAL INFILE "D:/data.txt" INTO TABLE tname; /* Lo ...

  4. 全选,不选,反选js

    <!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...

  5. VC 解密OUTLOOK pop3保存注册表密码

    原文连接:https://forum.90sec.org/forum.php?mod=viewthread&tid=8410 作者:Agile 用过OUTLOOK的人都知道,OUTLOOK的密 ...

  6. rdp爆破工具 Fast RDP Brute

    http://stascorp.com/load/1-1-0-58 Fast RDP Brute dservers.ru/wp-content/uploads/2013/11/frdpb2.zip

  7. 关于Response.redirect()方法

    1. sendRedirect 后面要加上return.2. sendRedirect 执行过程是先转向还是先执行后续代码再转向?答: 先执行代码再转向,在一个sendRedirect后面不能再有其他 ...

  8. IOS第七天(6:UiTableView编辑模式, 拖动位置 ,滑动删除)

    **********UiTableView编辑模式, 拖动位置 ,滑动删除 #import "HMViewController.h" @interface HMViewContro ...

  9. scp使用加密算法报错unknown cipher type

    为了提高scp的传输速度指定了scp的加密算法为arcfour $ scp -c arcfour localFile userName@remoteIP:remoteFile 得到报错unknown ...

  10. php读取文件里面的数组做为配置文件

    可能大家也都见过很多开源的产品,大多它们的配置文件都存放在一个单独的文件中,而这个文件里只存放了一个数组,其实这里运用了一个PHP的小技巧,就是可以将文件包含进来,并且赋值给一个变量,这个变量就具有了 ...