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

题目没有说所有间隔的start是依次增加的。所以,为了方便讨论,我们要将所有间隔按照start升序排列。因为间隔增加,就只需讨论end和下一个间隔。
结果集中是没有交叉出现的,排序后,遍历下一个时,只会跟结果集最后一个产生交叉,而不会跟在前的结果产生交叉(因为结果集中,最后一个间隔的start>前一个的end)
排列结束后,我们只需要讨论后一个跟前一个结果,如果后一个间隔的start大于前一个结果集中的间隔的end,那么没有交叉,直接添加 结果集。
否则该间隔就和结果集中最后一个间隔有交叉,这时要讨论该间隔的end和结果集中最后一个间隔的end大小,前者大,就要删除结果集最后一个,然后将前者加入结果集;如果后者大,说明只是end和start交叉了,这样删除结果集最后一个,添加新的间隔。见代码。

/**
* 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; }
* }
*/
class Solution {
public List<Interval> merge(List<Interval> intervals) {
List<Interval> res=new ArrayList<Interval>();
if(intervals==null||intervals.size()<=1) return intervals; Collections.sort(intervals,new Comparator<Interval>(){
public int compare(Interval o1,Interval o2){
return o1.start-o2.start;
}
});
res.add(intervals.get(0));
for(int i=1;i<intervals.size();i++){
Interval in1=res.get(res.size()-1); //结果集中最后一个间隔,用于比较
Interval in2=intervals.get(i); //当前间隔,只会与结果集中最后一个间隔产生交集。
if(in1.end<in2.start) res.add(in2); //没有交叉。直接加入
else { //有交叉
if(in1.end>=in2.end) continue;//前一个包含后一个,跳过
else{ //只是start和end交叉
res.remove(res.size()-1);
res.add(new Interval(in1.start,in2.end));
}
}
}
return res;
}
}

merge intervals(合并间隔)的更多相关文章

  1. [LeetCode] Merge Intervals 合并区间

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

  2. [LeetCode] 56 - Merge Intervals 合并区间

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

  3. 056 Merge Intervals 合并区间

    给出一个区间的集合, 请合并所有重叠的区间.示例:给出 [1,3],[2,6],[8,10],[15,18],返回 [1,6],[8,10],[15,18].详见:https://leetcode.c ...

  4. Leetcode56. Merge Intervals合并区间

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

  5. LeetCode 56. Merge Intervals 合并区间 (C++/Java)

    题目: Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6] ...

  6. 【LeetCode每天一题】Merge Intervals(合并区间)

    Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...

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

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

  8. 【leetcode】Merge Intervals

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

  9. 【leetcode】 Merge Intervals

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

随机推荐

  1. (一一三)使用系统自带框架操作SQLite3数据库

    系统自带的框架是基于C语言的,使用比较繁琐. 下面是使用步骤: 首先导入libsqlite3.0.dylib. ①在Document目录下打开数据库,如果没有则创建. NSString *sqlite ...

  2. (一一〇)正则表达式的基本使用与RegexKitLite的使用

    正则表达式常常用于匹配关键字,下面先介绍基本语法. [基本语法] ①中括号表示满足其中之一即可,例如[abc],则这个位置可以是a.b.c中任意一个. ②在中括号中,可以通过-连接范围,例如a-z:多 ...

  3. 后端分布式系列:分布式存储-HDFS DataNode 设计实现解析

    前文分析了 NameNode,本文进一步解析 DataNode 的设计和实现要点. 文件存储 DataNode 正如其名是负责存储文件数据的节点.HDFS 中文件的存储方式是将文件按块(block)切 ...

  4. SDL2源代码分析3:渲染器(SDL_Renderer)

    ===================================================== SDL源代码分析系列文章列表: SDL2源代码分析1:初始化(SDL_Init()) SDL ...

  5. [Linux]vbox 虚拟机添加新磁盘

    情况是这样的,开始创建虚拟机的时候硬盘设置太小了,只有10g,我现在通过vbox的设置给这个linux(centos6.6)虚拟机添加了一块硬盘. 下面的操作就是怎么把硬盘挂载到系统中. 通过 fdi ...

  6. 【一天一道LeetCode】#111. Minimum Depth of Binary Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  7. 【一天一道LeetCode】#104. Maximum Depth of Binary Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...

  8. zookeeper学习总结

    最近一两天,一直在看zookeeper,自己也感觉头昏脑涨的. 现记录一下,最近的所得: 安装与配置: http://blog.csdn.net/morning99/article/details/4 ...

  9. Socket编程实践(11) --epoll原理与封装

    常用模型的特点 Linux 下设计并发网络程序,有典型的Apache模型(Process Per Connection,PPC), TPC(Thread Per Connection)模型,以及 se ...

  10. 敏捷测试(5)--基于story的敏捷基础知识

    基于story的敏捷基础知识----需求管理(二) (1)定期发布 定期发布上线,把整个项目划分为一个个迭代,每个迭代时间大小固定(基本固定),迭代结束时上线交付一次. (2)迭代规划 迭代规划相当于 ...