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].


题目标签:Array

  这道题目给了我们一个区间的list,让我们返回一个list,是合并了所有有重叠的区间之后的list。这道题目的关键在于如何判断两个区间有重叠,根据原题给的例子可以看出,在按照每个区间的start排序之后很容易判断出是否相邻的两个区间有交集。我们看第一个区间[1,3] 中的end 3 > 第二个区间[2,6] 的start 2。在排序完之后,只要前面一个区间的end 大于等于 后面一个区间的start,它们就是重叠的。那么为了merge 这两个区间,保留第一个区间的start, 在两个end里面拿大的。设一个temp 等于第一个区间,遍历剩下的区间。然后每次拿temp 和这个区间比较,要注意的是,如果遇到了重叠的,把temp 更新为merge 后的区间,这时不需要加入list,因为还有可能继续和下一个区间重叠。当temp和这个区间不重叠的时候,把temp 加入list,再把这个区间设为新的temp。

Java Solution:

Runtime beats 89.25%

完成日期:07/20/2017

关键词:Array

关键点:利用comparator sort list,当A的end 大于等于 B的start,说明重叠

 /**
* 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> res = new ArrayList<>();
if(intervals.size() == 0)
return res; // sort the intervals list according to start
Collections.sort(intervals, new Comparator<Interval>(){
public int compare(Interval a, Interval b)
{
return a.start - b.start;
}
});; // get first one
Interval temp = intervals.get(0); // if intervals only has one element
if(intervals.size() == 1)
{
res.add(temp);
return res;
} // iterate intervals from [1] to end
for(int i=1; i<intervals.size(); i++)
{
// case 1: if temp interval end >= this interval start ->
// merge tempStart, max(tempEnd, thisEnd) and make this merge one as new temp;
if(temp.end >= intervals.get(i).start)
{
temp.end = Math.max(temp.end, intervals.get(i).end);
}
// case 2: if temp interval is not overlapping this interval ->
// add temp into list and make this new temp
else
{
res.add(temp);
temp = intervals.get(i);
}
}
// add the last temp into list
res.add(temp); return res;
}
}

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 56. Merge Intervals (合并区间)的更多相关文章

  1. [LeetCode] 56 - Merge Intervals 合并区间

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

  2. LeetCode 56. Merge Intervals 合并区间 (C++/Java)

    题目: Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6] ...

  3. [leetcode]56. Merge Intervals归并区间

    Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...

  4. leetcode 56. Merge Intervals 、57. Insert Interval

    56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...

  5. [LeetCode] Merge Intervals 合并区间

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

  6. 【LeetCode每天一题】Merge Intervals(合并区间)

    Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...

  7. 056 Merge Intervals 合并区间

    给出一个区间的集合, 请合并所有重叠的区间.示例:给出 [1,3],[2,6],[8,10],[15,18],返回 [1,6],[8,10],[15,18].详见:https://leetcode.c ...

  8. LeetCode: 56. Merge Intervals(Medium)

    1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域 ...

  9. Leetcode56. Merge Intervals合并区间

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

随机推荐

  1. Struts第八篇【资源国际化、对比JSP的资源国际化】

    资源国际化 我们在学JSTL标签的时候就涉及到了资源国际化,,,但是呢,在JSP的章节我并没有写博文来讲解怎么弄-.一方面感觉JSP的资源国际化好麻烦,另一方面是感觉用的地方很少-..因此就没有深入去 ...

  2. 【SQL】- 基础知识梳理(三) - SQL连接查询

    一.引言 有时为了得到一张报表的完整数据,需要从两个或更多的表中获取结果,这时就用到了"连接查询". 二.连接查询 连接查询的定义: 数据库中的表通过键将彼此联系起来,从而获取这些 ...

  3. ui-router

    学习历程:1 ng-router --> 2 location  --> 3 $location -->  4 promise  --> 5 html5 history  -- ...

  4. xgboost安装指南(win10,win7 64位)

    ---恢复内容开始--- Win7 64位系统下安装XGBoost 1. 环境介绍 计算机系统:win7 64位 Xgboost版本:xgboost0.6 2. 依赖软件环境 1) python 64 ...

  5. hibernate学习手记(1)

    1. java.sql.SQLException: The server time zone value '?й???????' is unrecognized or represents more ...

  6. 使用百度ueditor的插件使得代码高亮显示

    一.在show.html模板中,引入ueditor的插件,并调用 <link rel="stylesheet" href="__ROOT__/Data/uedito ...

  7. [Oracle]理解undo表空间

    一.回退段介绍 在Oracle数据库中,当某个事物对数据进行修改时,Oracle首先将数据的原始值保存到一个回退段中.一个事物只能将它的回退信息保存到一个回退段中,而多个并行事物可以使用同一个回退段. ...

  8. 聊聊Java语言中的单例

    package com.xinke.mybatis.test; public class TestSingleton { private static TestSingleton ts = null; ...

  9. Python自学笔记-Django分页器小实例

    from django.core.paginator import Paginator iter = 'abcdefhijklmnopqw' paginator = Paginator(iter,4) ...

  10. spring cloud+dotnet core搭建微服务架构:配置中心(四)

    前言 我们项目中有很多需要配置的地方,最常见的就是各种服务URL地址,这些地址针对不同的运行环境还不一样,不管和打包还是部署都麻烦,需要非常的小心.一般配置都是存储到配置文件里面,不管多小的配置变动, ...