题目:

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

链接: http://leetcode.com/problems/merge-intervals/

题解:

使用Comparator来对集合进行sort。 Comparator和Comparable是两个很重要的interface,再加上interable,runnable等,要好好掌握。

/**
* 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 == null || intervals.size() == 0)
return res;
Collections.sort(intervals, new IntervalComparator()); Interval last = intervals.get(0); for(int i = 1; i < intervals.size(); i++) {
Interval curr = intervals.get(i);
if(last.end < curr.start) {
res.add(last);
last = curr;
} else {
if(last.end < curr.end)
last.end = curr.end;
}
} res.add(last);
return res;
} public class IntervalComparator implements Comparator<Interval> {
@Override
public int compare(Interval a, Interval b) {
return a.start - b.start;
}
}
}

二刷:

Java:

依然是使用comparator来对整个interval数组进行排序,我们可以用anonymous comparator,或者是Java 8的lambda表达式。 下次把start重命名为prev可能更好一些。

使用Lambda:

Time Complexity - O(nlogn),  Space Complexity - O(1)

/**
* 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 == null || intervals.size() == 0) {
return intervals;
}
Collections.sort(intervals, (Interval i1, Interval i2) -> (i1.start != i2.start ? i1.start - i2.start : i1.end - i2.end));
Interval start = intervals.get(0);
for (int i = 1; i < intervals.size(); i++) {
Interval tmp = intervals.get(i);
if (start.end < tmp.start) {
res.add(start);
start = tmp;
} else if (start.end < tmp.end){
start.end = tmp.end;
}
}
res.add(start);
return res;
}
}

使用Anonymous comparator:

Time Complexity - O(nlogn),  Space Complexity - O(1)

/**
* 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 == null || intervals.size() == 0) {
return intervals;
}
Collections.sort(intervals, new Comparator<Interval>() {
public int compare(Interval i1, Interval i2) {
return i1.start != i2.start ? i1.start - i2.start : i1.end - i2.end;
}
});
Interval start = intervals.get(0);
for (int i = 1; i < intervals.size(); i++) {
Interval tmp = intervals.get(i);
if (start.end < tmp.start) {
res.add(start);
start = tmp;
} else if (start.end < tmp.end){
start.end = tmp.end;
}
}
res.add(start);
return res;
}
}

Updated:

class Solution {
public int[][] merge(int[][] intervals) {
if (intervals == null || intervals.length == 0) return new int[][] {};
Arrays.sort(intervals, (int[] i1, int[] i2) -> i1[0] != i2[0] ? i1[0] - i2[0] : i1[1] - i2[1]);
List<int[]> res = new ArrayList<>(); int[] lastInterval = intervals[0]; for (int i = 1; i < intervals.length; i++) {
if (intervals[i][0] <= lastInterval[1]) {
lastInterval[1] = Math.max(lastInterval[1], intervals[i][1]);
} else {
res.add(new int[]{lastInterval[0], lastInterval[1]});
lastInterval = intervals[i];
}
}
res.add(new int[]{lastInterval[0], lastInterval[1]});
return res.toArray(new int[res.size()][2]);
}
}

  

Reference:

https://leetcode.com/discuss/13953/a-simple-java-solution

https://leetcode.com/discuss/33434/a-clean-java-solution

https://leetcode.com/discuss/42344/7-lines-easy-python

https://leetcode.com/discuss/43383/easy-c-solution-with-explanations-o-nlogn-time-and-o-n-space

56. Merge Intervals的更多相关文章

  1. [Leetcode][Python]56: Merge Intervals

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...

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

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

  3. 刷题56. Merge Intervals

    一.题目说明 题目是56. Merge Intervals,给定一列区间的集合,归并重叠区域. 二.我的做法 这个题目不难,先对intervals排序,然后取下一个集合,如果cur[0]>res ...

  4. 56. Merge Intervals - LeetCode

    Question 56. Merge Intervals Solution 题目大意: 一个坐标轴,给你n个范围,把重叠的范围合并,返回合并后的坐标对 思路: 先排序,再遍历判断下一个开始是否在上一个 ...

  5. 56. Merge Intervals 57. Insert Interval *HARD*

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

  6. 【LeetCode】56. Merge Intervals

    Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given  ...

  7. Leetcode#56 Merge Intervals

    原题地址 排序+合并,没啥好说的 第一次尝试C++的lambda表达式,有种写js的感觉,很神奇 c11就支持了lambda表达式,仔细想想,我学C++大概就是在09~10年,c11还没有发布,不得不 ...

  8. [LeetCode] 56. Merge Intervals 解题思路

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

  9. LeetCode 56. Merge Intervals (合并区间)

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

随机推荐

  1. DataSnap如何监控Tcp/IP客户端的连接情况

    一个实例,如果客户端是TCP/IP是短连接的情况就没有必要了. 一.GlobVar.pas单元,定义应用系统全局数据类型及变量: unit GlobVar; interface uses System ...

  2. How to add EDT relation table[AX2012]

    setp 1. First create New Edit. setp 2.Create New Table First Table Name is NParentRel then drag and ...

  3. oracle作业

    http://blog.csdn.net/hao_ds/article/details/38382931 oracle作业各种参数的详细介绍

  4. 微软职位内部推荐-SDE2 (Windows driver)

    微软近期Open的职位: SDE2 (Windows driver) Job title: Software Development Engineer 2 Location: Shanghai, Ch ...

  5. SQL Server DATEADD() 函数

    SQL Server Date 函数 定义和用法 DATEADD() 函数在日期中添加或减去指定的时间间隔. 语法 DATEADD(datepart,number,date) date 参数是合法的日 ...

  6. LintCode-Unique Path II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  7. 2436: [Noi2011]Noi嘉年华 - BZOJ

    Description NOI2011 在吉林大学开始啦!为了迎接来自全国各地最优秀的信息学选手,吉林大学决定举办两场盛大的 NOI 嘉年华活动,分在两个不同的地点举办.每个嘉年华可能包含很多个活动, ...

  8. Eclipse中创建标准web工程以及标准目录结构说明

    最近公司有个Web项目,项目结构如下: 虽然运行没有错,但是实在是别扭,标准的web应用一般不采用这种结构: 因此总结一下:     1.如何在Eclipse中创建一个标准的Web应用.     2. ...

  9. ExtJS4.2学习(21)动态菜单与表格数据展示操作总结篇2

    运行效果: 此文介绍了根据操作左侧菜单在右面板展示相应内容. 一.主页 先看一下跳转主页的方式:由在webapp根目录下的index.jsp跳转至demo的index.jsp 下面是demo的inde ...

  10. 编写一个递归函数,输出vector对象的内容

    // test14.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...