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 ...
随机推荐
- vscode:配置git
1.下载git,并找到安装git的文件位置,并找到git文件夹下面的Bin文件夹 2.配置环境变量 位置:右击‘计算机’->属性->高级系统设置->环境变量 最后点击确定即可 ...
- 有趣的IntegerCache
一个很有趣的现象,下面这两个结果输出的结果是false true,这是为什么? 翻看Integer的源码可以看到,当new Integer(12);时,没有什么特别的,就是通过构造方法创建了一个I ...
- Github注册
官网:https://github.com/ sign in : 登录. sign up: 注册. 注意邮箱不可以随便乱填,邮箱用于激活账号和找回密码. 由于github的服务器在国外,访问比较慢,需 ...
- CF587F Duff is Mad
题目 有趣的思想 首先暴力的话,自然是对每一个询问在\(AC\)自动机上跑一遍\(k\),看看跑出来的节点在\(fail\)树到根的路径上有多少个\(l\)到\(r\)之间的结束标记就好了 我们发现无 ...
- java 编写小工具 尝试 学习(一)
1.单片机 调试经常 需要 用 串口 工具 发送 一些 特定的 协议或者 命令,每次要 翻译 写成 2进制 很麻烦 ,因此 打算自己用 java 写一个 工具 方便自己 调试,2017年3月2 ...
- SignalR 教程一
转帖官方教程:Tutorial: Getting Started with SignalR 2 and MVC 5 http://www.asp.net/signalr/overview/gettin ...
- Oracle 体系结构一 概述
Oracle服务器由两个实体组成:实例和数据库. 实例由内存结构和进程组成. 它暂时存在于RAM和CPU中.当关闭运行的实例时,实例将消失的无影无踪. 数据库由磁盘上的文件组成.不管在运行状态还是停止 ...
- UIPanGestureRecognizer 拖动TableView改变其高度
需求:项目中要求tableView的高度随着手拖动的位置而改变如下图: 关键代码如下: - (void)viewDidLoad{ panGestureRecognizer = [[UIPanGestu ...
- DB数据源之SpringBoot+MyBatis踏坑过程(六)mysql中查看连接,配置连接数量
DB数据源之SpringBoot+MyBatis踏坑过程(六)mysql中查看连接,配置连接数量 liuyuhang原创,未经允许禁止转载 系列目录连接 DB数据源之SpringBoot+Mybati ...
- 解决vue变量未渲染前代码显示问题
在网络加载缓慢或者刷新的时候总会有那么一瞬间出现vue的模板代码,实在很影响美观,对于我这种有强迫症的人来说实在是忍无可忍,后来经过查找资料,终于发现了解决方法,可以使用vue现成的指令来解决这个问题 ...