LeetCode: Merge Intervals 解题报告

Merge Intervals
Given a collection of intervals, merge all overlapping intervals.
For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].
SOLUTION 1:
1. 先使用Comparator 的匿名类对intervels进行排序。
2. 把Intervals遍历一次,依次一个一个merge到第1个interval。 把第1个interval设置为last(最后一个加到结果集里)
有2种情况:
(1) cur在last的后面。把last加到结果集,将cur加到结果集中。
(2)cur与last有重合部分,把它们合并,合并之后的区间作为新的last.
所有的都扫完后,把last加到结果集中即可。
注意:因为现在Leetcode所有入参都搞成了List,所以遍历时最好使用Iterator,这样无论对于Linkedlist,还是arraylist,性能都是一样的,否则使用get(i)对于LinkedList会相当的缓慢,就不是O(1)的时间了。
复杂度:排序是NlogN, 而merge本身是N。所以总体时间复杂度是NlogN
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public List<Interval> merge(List<Interval> intervals) {
List<Interval> ret = new ArrayList<Interval>();
if (intervals == null || intervals.size() == 0) {
return ret;
} Collections.sort(intervals, new Comparator<Interval>() {
public int compare(Interval o1, Interval o2) {
// sort the intervals by the start.
return o1.start - o2.start;
}
}); // 作为最后一个插入的区间
Interval last = intervals.get(0); // 这里要考虑性能。使用iterator的话,对linkedlist会更快.
Iterator<Interval> itor = intervals.iterator();
while (itor.hasNext()) {
Interval cur = itor.next();
// cur 在last的右边
if (cur.start > last.end) {
// 将cur作为新的last.
ret.add(last);
last = cur;
// cur与last有重合的部分,合并之
} else {
int s = last.start;
int e = Math.max(last.end, cur.end);
last = new Interval(s, e);
}
} // 把最后一个区间加上
ret.add(last); return ret;
}
}
REF: http://blog.csdn.net/fightforyourdream/article/details/16882295
LeetCode: Merge Intervals 解题报告的更多相关文章
- 【LeetCode】56. Merge Intervals 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】435. Non-overlapping Intervals 解题报告(Python)
[LeetCode]435. Non-overlapping Intervals 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- LeetCode - Course Schedule 解题报告
以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- [LeetCode] Merge Intervals 排序sort
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- [LeetCode] 56. Merge Intervals 解题思路
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 【LeetCode】915. Partition Array into Disjoint Intervals 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/partitio ...
随机推荐
- ubuntu开机自动启动xampp/lampp的两种方法
方法一: sudo ln -s /opt/lampp/lampp /etc/init.d/lampp sudo update-rc.d -f lampp defaults 方法二: 1. 在/etc ...
- 山东省赛-博弈-Game
id=1582" target="_blank" style="font-size:18px">点击打开题目链接 非常明显的一道博弈题目,可 ...
- 开源大数据技术专场(下午):Databircks、Intel、阿里、梨视频的技术实践
摘要: 本论坛第一次聚集阿里Hadoop.Spark.Hbase.Jtorm各领域的技术专家,讲述Hadoop生态的过去现在未来及阿里在Hadoop大生态领域的实践与探索. 开源大数据技术专场下午场在 ...
- 原创+转发:微信小程序navigator、redirectTo、switchTab几种页面跳转方式
什么是事件? 事件是视图层到逻辑层的通讯方式. 事件可以将用户的行为反馈到逻辑层进行处理. 详解(以常见的tap点击事情为例) 模板.wxml代码: <view id="tapTest ...
- HDUOJ----1114(多重背包)悼念512汶川大地震遇难同胞——珍惜现在,感恩生活
悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- poj-------Common Subsequence(poj 1458)
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 34477 Accepted: 13 ...
- CentOS6.6 32位 Minimal版本纯编译安装Nginx Mysql PHP Memcached
声明:部分编译指令在博客编辑器里好像被处理了,如双横线变成单横线了等等,于是在本地生成了一个pdf版本,在下面地址可以下载. LNMP+Memcached CentOS是红帽发行的免费的稳定Linux ...
- Redis的Docker镜像
原文地址:https://hub.docker.com/_/redis/ Pull Command docker pull redis Short Description Redis is an op ...
- Python学习笔记010——形参与实参
在使用中忽略了一个问题,形参有些和实参类似,也不能是“关键字后面含有位置参数”,即“默认形参”后面必须不能含有“位置”形参! def test(a=100,b): print("test&q ...
- Microsoft Excel不能访问文件
Microsoft Excel 不能访问文件"C:\Users\james\Documents\test.xls". 可能的原因有以下几个: • 文件名称或路径不存在. • 文件正 ...