题目:

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. 1102. Invert a Binary Tree (25)

    The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...

  2. gif修改背景透明

    1.用ImageReady打开,将选中所有帧,右键选择“恢复为背景”. 2.打开“颜色板”,点击左下角的惊叹号,用吸色器点击背景,颜色板自动选中了背景色,将其映射为透明. 3.文件->将优化结果 ...

  3. EF中使用Select new 方法中字段值替换的问题

    前提需要替换查询得到的List当中的某个字段的值,替换规则有一个mapping关系 尝试代码 有问题 无法获取任何数据 /// <summary> /// 获取Treegrid的List ...

  4. java之redis篇(spring-data-redis整合)

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  5. iOS 系统二维码扫描(可限制扫描区域)

    使用 AVFoundation系统库来进行二维码扫描并且限制扫描二维码的范围.(因为默认的是全屏扫描) -(void)beginCode { //1.摄像头设备 AVCaptureDevice *de ...

  6. what are Datatypes in SQLite supporting android

    As said at Datatypes In SQLite Version 3: Datatypes In SQLite Version 3 Most SQL database engines (e ...

  7. C#序列化与反序列化(Serialize,Deserialize)实例详解

    这篇文章主要介绍了C#序列化与反序列化(Serialize,Deserialize)的方法,实例分析了C#序列化与反序列化的常见技巧,需要的朋友可以参考下 本文实例讲述了C#序列化与反序列化(Seri ...

  8. 【BZOJ】【2844】albus就是要第一个出场

    高斯消元解XOR方程组 srO  ZYF  Orz 膜拜ZYF…… http://www.cnblogs.com/zyfzyf/p/4232100.html /******************** ...

  9. win8双屏敲代码

    23寸,AOC冠杰("AOC I2369V 23英寸LED背光超窄边框IPS广视角液晶显示器(银色)") 某东,920买入.

  10. 解决eclipse打开报错:failed to create the java virtual ma

    在Eclipse安装目录下找到:eclipse.ini 将如下参数改为: --launcher.XXMaxPermSize 128M ------------------------------- 说 ...