Description

Given a collection of intervals, merge all overlapping intervals.

Example

Given intervals => merged intervals:

[                     [
(1, 3), (1, 6),
(2, 6), => (8, 10),
(8, 10), (15, 18)
(15, 18) ]
]

Challenge

O(n log n) time and O(1) extra space.

题意:给定一个集合,里面有若干无序区间,要求将有重叠部分的区间合并。这个题目的示例给的不是很好,这个示例给人的感觉好像这些区间是有序的。有序和无序,用同样的方法做,结果可能不一样,比如我一开始理解成有序,报错如下:

Input
[(2,3),(4,5),(6,7),(8,9),(1,10)]
Output
[(2,3),(4,5),(6,7),(1,10)]
Expected
[(1,10)]
Hint
Review your code and make sure your algorithm is correct. Wrong answer usually caused by typos if your algorithm is correct.
Input test data (one parameter per line.)

虽然它本来无序,但我们也可以人为地将它根据first值的大小进行排序,可以使用Collections类中的sort方法(查一下API)对List进行排序。排完之后,就可以对集合内的区间进行合并了。合并的方法与此题类似:30. Insert Interval【LintCode by java】

申请一个新的集合,再用一个循环,将排好序的区间两两比较,如果无需合并,则将前者加入新的集合,后者继续与后面的区间比较合并。代码如下:

 public class Solution {
/**
* @param intervals: interval list.
* @return: A new interval list.
*/
//判断两区间是否相交
public List<Interval> merge(List<Interval> intervals) {
// write your code here
if(intervals.size()==0||intervals.size()==1)
return intervals;
List<Interval>res=new ArrayList<Interval>();
Collections.sort(intervals,new IntervalCompare());
Interval last=intervals.get(0);
for(int i=1;i<intervals.size();i++){
Interval cur=intervals.get(i);
if(last.end<cur.start){
res.add(last);
last=cur;
}else{
last.start=Math.min(last.start,cur.start);
last.end=Math.max(last.end,cur.end);
}
}
res.add(last);
return res;
}
private class IntervalCompare implements Comparator<Interval>{
public int compare(Interval a,Interval b){
return a.start-b.start;
}
}
}

如有错误,欢迎批评指正~

156. Merge Intervals【LintCode by java】的更多相关文章

  1. 30. Insert Interval【LintCode by java】

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

  2. 165. Merge Two Sorted Lists【LintCode by java】

    Description Merge two sorted (ascending) linked lists and return it as a new sorted list. The new so ...

  3. 156. Merge Intervals【easy】

    Given a collection of intervals, merge all overlapping intervals.   Example Given intervals => me ...

  4. 212. Space Replacement【LintCode by java】

    Description Write a method to replace all spaces in a string with %20. The string is given in a char ...

  5. 158. Valid Anagram【LintCode by java】

    Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification ...

  6. 177. Convert Sorted Array to Binary Search Tree With Minimal Height【LintCode by java】

    Description Given a sorted (increasing order) array, Convert it to create a binary tree with minimal ...

  7. 173. Insertion Sort List【LintCode by java】

    Description Sort a linked list using insertion sort. Example Given 1->3->2->0->null, ret ...

  8. 172. Remove Element【LintCode by java】

    Description Given an array and a value, remove all occurrences of that value in place and return the ...

  9. 155. Minimum Depth of Binary Tree【LintCode by java】

    Description Given a binary tree, find its minimum depth. The minimum depth is the number of nodes al ...

随机推荐

  1. Windows 实用小工具

    超实用的Windows工具 ====================================================================================== ...

  2. GO语言(八) defer注意点

    package main import ( "net" "os" "fmt" "io/ioutil" ) func Cl ...

  3. ZT 针对接口编程而不是针对实现编程

    java中继承用extends 实现接口用 implements 针对接口编程而不是针对实现编程 2009-01-08 10:23 zhangrun_gz | 分类:其他编程语言 老听说这句,不知道到 ...

  4. Yii 不完全解决方案(一)

    此文意在记录 Yii 开发过程中的小问题解决方案 1. Yii 中 Js 和 Css 文件的引入. 我们就从最简单的问题开始吧,说起来也不是问题,只是语法罢了.假设我们的 js 文件都放在和 prot ...

  5. luogu P1462 通往奥格瑞玛的道路

    嘟嘟嘟 这道题的题面相当的迷,我看了半天都没懂.最后看了题解的解释才懂. 他是这么个意思:对于所有能活着走到终点的路径,输出每一条路径中过路费最多的城市的最小值. 那么自然想到二分过路费,然后用dij ...

  6. 访问服务器,远程访问linux主机

    ssh conch@+ip地址,输入密码后就可以访问并使用服务器了.登录服务器之后,xbwang@xbwang-desktop:~$变成了conch@conchdev:~$ ,这样你就可以像使用普通电 ...

  7. 【转】Linux如何查看JDK的安装路径

    http://www.cnblogs.com/kerrycode/archive/2015/08/27/4762921.html 如何在一台Linux服务器上查找JDK的安装路径呢? 有那些方法可以查 ...

  8. android中OpenMax的实现【3】OMX中主要成员

    原文 http://blog.csdn.net/tx3344/article/details/8117908 通过上文知道了,每个AwesomePlayer 只有一个OMX服务的入口,但是Awesom ...

  9. java 编写小工具 尝试 学习(三)

    1.在 新建的 窗口上  添加  按钮  ,不废话 ,贴代码和 截图 package jFrameDemo; import java.awt.event.ActionEvent; import jav ...

  10. JS其他类型值转化为Boolean类型规则

    由于最近在笔试的时候,发现好多关于其他类型转化为Boolean类型的题目,因此总结一下! 一.String类型转化为Boolean 1.转化结果为true的情况 ①任何非空字符串 PS:空白字符串也是 ...