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. malloc、calloc、relloc

    1.malloc void * malloc(size_t _Size); malloc函数在堆中分配参数_Size指定大小的内存,单位:字节,函数返回void *指针. 2.calloc void ...

  2. 8.非关系型数据库(Nosql)之mongodb的应用场景

     测试脚本: Mysql测试脚本: [php] view plaincopyprint? 1.  <?php 2.  header("Content-Type:text/html; ...

  3. (NO.00004)iOS实现打砖块游戏(五):游戏场景类

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 创建游戏场景类头文件 在Xcode创建新GameScene类,继 ...

  4. qualcomm memory dump 抓取方法

    Memory dump是系统出现crash时常用的分析故障原因的方法,qualcomm 各子系统运行时,为方便debug,都会开辟ram log和debug variable用于保存各系统运行信息及健 ...

  5. (六十四)iOS的socket实现(C+OC混合实现)

    对于微博.微信朋友圈之类的网络通信,使用JSON完全可以满足需求,但是如果要制作网络游戏,就需要建立一个持久连接,这时候就要考虑使用socket. 在iOS上实现socket大体有两种方法,一是借助自 ...

  6. 【Unity技巧】开发技巧(技巧篇)

    写在前面 和备忘录篇一样,这篇文章旨在总结Unity开发中的一些设计技巧,当然这里只是我通过所见所闻总结的东西,如果有不对之处欢迎指出. 技巧1:把全局常量放到一个单独的脚本中 很多时候我们需要一些常 ...

  7. HTML5 Web Storage 特性

    原文地址: Using HTML5 Web Storage 原文日期: 2010年06月28日 翻译日期: 2013年08月12日 当下Web开发领域最火爆的词语当属 HTML5.HTML5标准的新特 ...

  8. android:background="@color/white" [create file color.xml at res/values/]

     <resources><color name="white">#FFFFFF</color><!--白色 --><col ...

  9. Linux搭建GIT 使用Eclipse创建并上传Git项目 EGit操作

    Linux搭建Git 1. gitblit服务器文档 http://gitblit.com/setup_go.html 2. 安装jdk 参考 http://blog.csdn.net/jerome_ ...

  10. (NO.00002)iOS游戏精灵战争雏形(一)

    原本想做一个复杂点的平面动作游戏,可以觉得还是有点把握不了.还是先从简单的原型开始吧. 构思中的精灵战争(SpriteWar)是一个类似FC时代的小游戏,可以造兵,可以捕获敌兵.原本还想加上保卫老巢的 ...