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. 查看Oracle表中的指定记录在数据文件中的位置

    查看Oracle表中的指定记录位置select rowid,user_id from sshr.xx_user where user_id=3010586 select rowid,       db ...

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

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

  3. 实验5&期中考试后两题

    实验内容1: #include <iostream> #include <vector> #include <string> using namespace std ...

  4. 第三篇——第二部分——第一文 SQL Server镜像简单介绍

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/DBA_Huangzj/article/details/26951563 原文出处:http://bl ...

  5. Android中跑马灯效果

    <com.randy.test1.self.MarqueeText android:id="@+id/btn1" android:layout_width="mat ...

  6. 浅谈对MJRefresh(上)下拉刷新控件的理解

    MJRefresh GitHub地址:https://github.com/CoderMJLee/MJRefresh 利用业余时间研究了一下iOS的开发,发现OC特定的语法方式吸引了我,而且iOS开发 ...

  7. 解决vue-router中this.$router.push无法在新窗口中打开

    解决vue-router中this.$router.push无法在新窗口中打开 let routeData = this.$router.resolve({ path: '/consult', que ...

  8. 配置RedisTemplate、JedisPoolConfig、JedisConnectionFactory+自定义序列化 (xml+java方式)+使用

    java方式配置RedisTemplate //spring注入ben //@Bean(name = "redisTemplate") public RedisTemplate i ...

  9. 什么是mysql的事务和实现

    msql的一个事务的回归测试,可以自测一下,了解下事务. 举个例子:小明和小飞两个人现在手里各有¥100,突然小飞脑袋出问题了说给小明¥50,现在他们手里的钱就是(小明:¥150,小飞:¥50):这样 ...

  10. STC12LE5620AD RAM问题

    1.此款单片机内部有 sram:768B=512B(aux)+256B(Internal) 2.内部RAM解析 2. 3.内部扩展RAM 4.keil中可以选择内存类型 5. 网上摘抄的一段话: 在S ...