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 ...
随机推荐
- 在ubuntu16.04上安装eclipse
在ubuntu16.04上安装eclipse 一.下载 首先我们需要安装jdk1.8及其以上,然后从官网:https://www.eclipse.org/downloads/上下载,需要注意 ...
- 【2017.09.15 智能驾驶/汽车电子】汽车高级驾驶辅助ADAS常用传感器厂商:激光雷达篇
不定期更新,主要是汇总Internet上的激光雷达厂商资讯,不涉及技术,以备参考. 1. Innoviz:固态激光雷达公司 新闻链接:http://36kr.com/p/5092055.html 激光 ...
- Linux汉化(Cent Os汉化)
在腾讯云上购买了Cent Os7.1的云服务器,是英文版啊,有没有?对于我这种英文的渣渣啊,所以我要用中文版,我就是这么low,怎么着呢? Ok ,在汉化之前,先查看系统的语言环境, echo $LA ...
- [原]SDL开发教程
SDL开发库:http://www.libsdl.org/ SDL中文开发教程:http://tjumyk.github.io/sdl-tutorial-cn/index.html SDL英文版开发教 ...
- UVa 658 - It's not a Bug, it's a Feature!(Dijkstra + 隐式图搜索)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 【[HNOI2006]超级英雄】
看到楼下有大佬说了网络流做法,来给大佬配个代码 我们只有我可能都觉得如果不动态加边的话\(dinic\)可能跑不了这种需要中途退出的二分图匹配 正当我准备去敲匈牙利的时候突然想到这个题可以二分啊 于是 ...
- idea中使用git进行clone、分支切换、pull、push等
转:https://blog.csdn.net/xiaobai__lee/article/details/81081128 https://blog.csdn.net/geng31/article/d ...
- 批量压缩文件夹到Zip文件
实现效果: 实现代码:
- 2019.1.7 Mac的Vscode插件总结
Vscode插件 通用插件 Chinese 配置中文界面 HTML Snippets H5代码片段以及提示 HTML CSS Support 让 html 标签上写class 智能提示当前项目所支持的 ...
- mvc读书笔记
在mvc3的時候引入了Razor.Mvc4中默認的頂級目錄/controllers 保存那些處理URL請求的controller類/models 保存那些表示和操縱數據以及業務對象的類/views 保 ...