156. Merge Intervals【LintCode by java】
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】的更多相关文章
- 30. Insert Interval【LintCode by java】
Description Given a non-overlapping interval list which is sorted by start point. Insert a new inter ...
- 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 ...
- 156. Merge Intervals【easy】
Given a collection of intervals, merge all overlapping intervals. Example Given intervals => me ...
- 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 ...
- 158. Valid Anagram【LintCode by java】
Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification ...
- 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 ...
- 173. Insertion Sort List【LintCode by java】
Description Sort a linked list using insertion sort. Example Given 1->3->2->0->null, ret ...
- 172. Remove Element【LintCode by java】
Description Given an array and a value, remove all occurrences of that value in place and return the ...
- 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 ...
随机推荐
- MYSQL 升序排序但值为0的排最后
转载 http://blog.csdn.net/looksun/article/details/51445205 如一张表的数据如下: 需要根据gz列的值进行升序排序,但值为0的排在最后面,即最终结果 ...
- Windows 10 X64 ISO 专业版&家庭版下载与永久激活
好久没有更新博客,算算时间,已经有4年了,好吧,今天突发奇想,想把今天安装Windows 10的过程给记录下来. 2015年的时候,微软就发布了Windows 10,当时正版的Win7.Win8都可以 ...
- unity3d中设计模式的学习<一>:泛型单例
单例是游戏开发中比较常见的设计模式,虽然针对的功能不同,但是有一些功能还是共有的,代码也不少,如果能放在一个基类里面是最好不过了,但是单例里需要有个instance功能来返回当前对象,所以这个功能必须 ...
- 一对一关联关系基于主键映射的异常 IdentifierGenerationException
具体异常:org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one pro ...
- Avito Cool Challenge 2018 C. Colorful Bricks 【排列组合】
传送门:http://codeforces.com/contest/1081/problem/C C. Colorful Bricks time limit per test 2 seconds me ...
- LayIM.NetClient 组件开发记录
前言 好久没写博客了.前阶段看了下Hangfire组件,后来对其代码比较感兴趣,当时不太了解他如何生成的页面和一些访问请求等.后来看了下源代码,发现原来是 OWIN 在搞怪.于是乎开始深入研究Hang ...
- es6之数组方法
//兼容插件 babel-polyfill values()等存在兼容问题,需要加载babel-polyfill插件 .keys() 获取数组的key值 .values() 获取数组的value值 ...
- alibaba--java规范
18. [推荐]final 可以声明类.成员变量.方法.以及本地变量,下列情况使用 final 关键字: 1) 不允许被继承的类,如:String 类. 2) 不允许修改引用的域对象,如:POJO 类 ...
- git终端操作
1.提交 git add . git commit -m "test" git push origini master 2.分支 创建feature_x分支,并切换到feature ...
- NSArray中地内存管理 理解
问题: 通过alloc和init的方法创建了NSArray和NSDictionary,然后通过addobject和setobject:forkey:将object添加进去.通过addobject会自动 ...