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 ...
随机推荐
- Android Studio经常使用配置及使用技巧(二)
在<Android Studio经常使用配置及使用技巧(一)>中具体描写叙述了Android Studio的project结构和打开开源project的一些配置方法.本篇将从我个人的使用情 ...
- python学习笔记之基础数据和控制
注释: 单行注释 # 多行注释''' ''' 注意:当注释中有汉字时需要在python文件的第一行添加如下内容之一:#coding:gbk或#coding:utf-8或##-*- coding ...
- iOS - AsyncSocket 的使用
1.AsyncSocket 基于 CFSocket.GCD 进行的封装(OC). 支持 TCP 和 UDP. 完整的回调函数(用于处理各种回调事件,连接成功,断开连接,收到数据等). 需要注意的问题: ...
- ASP中页面之间传递值的几种方式
ASP.NET页面之间传递值的几种方式 页面传值是学习asp.net初期都会面临的一个问题,总的来说有页面传值.存储对象传值.ajax.类.model.表单等.但是一般来说,常用的较简单有QueryS ...
- python学习笔记014——错误和异常
Python有两种错误很容易辨认:语法错误和异常. 1 什么是语法错误 Python 的语法错误或者称之为解析错,是初学者经常碰到的,如下实例 if i>4 print("if语句输出 ...
- OAF_OAF控件系列5 - Train的实现(案例)
2014-06-02 Created By BaoXinjian
- OAF_Oracle Application Framework基本知识点(概念)
2014-02-06 Created By BaoXinjian
- Android多点触摸放大缩小图片
1.Activity package com.fit.touchimage; import android.app.Activity; import android.graphics.Bitmap; ...
- C++第15周(春)项目2 - 用文件保存的学生名单
课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759.内有完整教学方案及资源链接 本程序中须要的相关文件.请到http://pa ...
- Openresty增加waf配置
Openresty增加waf配置 1. Ngx lua waf 说明 防止sql注入,本地包含,部分溢出,fuzzing测试,xss,SSRF等web攻击 防止svn/备份之类文件泄漏 防止Apach ...