题目:

合并区间

给出若干闭合区间,合并所有重叠的部分。

样例

给出的区间列表 => 合并后的区间列表:

[                     [
[1, 3], [1, 6],
[2, 6], => [8, 10],
[8, 10], [15, 18]
[15, 18] ]
]
挑战

O(n log n) 的时间和 O(1) 的额外空间。

解题:

按照start对区间进行排序

实现Comparator接口,重写compare方法

    private class IntervalComparator implements Comparator<Interval>{
public int compare(Interval a,Interval b){
return a.start - b.start;
}
}

对相邻的两个区间:last,cur.last在前,cur在后

if last.end >= cur.start 前一个区间的结束包括了后一个区间的开始:

  这两个区间需要合并,更新last.end = Max(last.end,cur.end)

else

  last 加入的 list中

  更新 list = cur

/**
* Definition of Interval:
* public class Interval {
* int start, end;
* Interval(int start, int end) {
* this.start = start;
* this.end = end;
* }
*/ class Solution {
/**
* @param intervals: Sorted interval list.
* @return: A new sorted interval list.
*/
public List<Interval> merge(List<Interval> intervals) {
// write your code here
if(intervals == null || intervals.size()<=1){
return intervals;
}
Collections.sort(intervals,new IntervalComparator());
List<Interval> result = new ArrayList<Interval>();
Interval last = intervals.get(0);
for(int i=1;i<intervals.size();i++){
Interval curt = intervals.get(i);
if(curt.start<=last.end){
last.end = Math.max(last.end,curt.end);
}else{
result.add(last);
last = curt;
}
}
result.add(last);
return result;
}
private class IntervalComparator implements Comparator<Interval>{
public int compare(Interval a,Interval b){
return a.start - b.start;
}
} }

参考链接:http://www.jiuzhang.com/solutions/merge-intervals/

lintcode:合并区间的更多相关文章

  1. 合并区间(LintCode)

    合并区间 给出若干闭合区间,合并所有重叠的部分. 样例 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8, 1 ...

  2. [LeetCode] Merge Intervals 合并区间

    Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...

  3. LeetCode(56):合并区间

    Medium! 题目描述: 给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18] ...

  4. HRBUST - 1818 石子合并 区间dp入门

    有点理解了进阶指南上说的”阶段,状态和决策“ /* 区间dp的基础题: 以区间长度[2,n]为阶段,枚举该长度的区间,状态dp[l][r]表示合并区间[l,r]的最小费用 状态转移方程dp[l][r] ...

  5. 【LeetCode】数组--合并区间(56)

    写在前面   老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...

  6. 合并区间 · Merge Intervals & 插入区间 · Insert Interval

    [抄题]: 给出若干闭合区间,合并所有重叠的部分. 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8, 10] ...

  7. leetcode合并区间

    合并区间     给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: ...

  8. HDU 3397 Sequence operation(区间合并 + 区间更新)

    题目链接:pid=3397">http://acm.hdu.edu.cn/showproblem.php?pid=3397 题意:给定n个数,由0,1构成.共同拥有5种操作. 每一个操 ...

  9. 『字符合并 区间dp 状压dp』

    字符合并 Description 有一个长度为 n 的 01 串,你可以每次将相邻的 k 个字符合并,得到一个新的字符并获得一定分数.得到的新字符和分数由这 k 个字符确定.你需要求出你能获得的最大分 ...

随机推荐

  1. Java内存溢出详解

    转自:http://elf8848.iteye.com/blog/378805 一.常见的Java内存溢出有以下三种: 1. java.lang.OutOfMemoryError: Java heap ...

  2. 常用的php数组排序函数

    分享几个php数组排序函数,每个函数出去sort是排序的意思前缀字母的含义分别代表: a 索引 k 数组键 r 逆向 u 用户自定义 顺序排序函数 sort — 对数组排序  ksort — 对数组按 ...

  3. centos 减少tty数量的方法

    在linux中,包括本文介绍的centos系统中,tty系统默认是给出7个,前六个是terminal,一个用于X. 在centos5.x中减少tty数量,通过修改/etc/inittab来实现. [r ...

  4. 有关Mysql连接问题

    问题一——Mysql number Error 2003 MySQL连接错误.错误代码10061.10061一般是Mysql服务没启动.或Mysql服务器无法连接 . 在程序栏找到Mysql\Mysq ...

  5. 分享:mysql 随机查询数据

    在mysql中查询5条不重复的数据,使用以下: 1 SELECT * FROM `table` ORDER BY RAND() LIMIT 5  就可以了.但是真正测试一下才发现这样效率非常低.一个1 ...

  6. GITHUB基础使用教程

    windows系统下:   1.安装完成后,还需要最后一步设置,在命令行输入: $ git config --global user.name "Your Name" $ git ...

  7. How to running Job from a Form

    For Example we wanna run a Job with name  "FAN_TableList_CSV". So you must create a button ...

  8. 如何在SAE上使用DjangoUeditor

    之前的一个项目是部署到SAE上的,其中富文本编辑器使用到了Ueditor,对于Django有一个开源项目DjangoUeditor可以简化继承过程,直接引用app就可以使用.但是不支持SAE的文件上传 ...

  9. 转学步园:jquery offset

    JQuery Offset实验与应用 我们有时候需要实现这样一种功能:点击一个按钮,然后在按钮的下方显示一个div.当按钮位于角落时,div的位置设定就需要计算,使div完全显示. 我打算使用offs ...

  10. return *this和return this的区别

    别跟我说, return *this返回当前对象, return this返回当前对象的地址(指向当前对象的指针). 正确答案为:return *this返回的是当前对象的克隆(当然, 这里仅考虑返回 ...