题目:

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. Elastix 禁用SSL(https),还原为 http 访问

    1.相关配置文件目录: Apache的配置文件:httpd.conf,位于:/etc/httpd/conf/httpd.conf,配置文件中包含 Include conf.d/*.conf ,引入了  ...

  2. DataTable中如何去除重复的项【转】

    上周在项目中遇到一个问题,就是获取DataTable中某一列的值,因为从数据库中检索数据时,按照2个字段进行分组,而要获得的那一列刚好在分组这两列中,所以该列的值必然有重复,于是就想到了去除重复,有了 ...

  3. oracle作业

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

  4. ios6 处理内存警告

    iPhone下每个app可用的内存是被限制的,如果一个app使用的内存超过20M,则系统会向该app发送Memory Warning消息.收到此消息后,app必须正确处理,否则可能出错或者出现内存泄露 ...

  5. 为aps.net core项目加上全局异常捕捉和记录

    在asp.net core中的方案在这里:http://stackoverflow.com/questions/30385246/can-asp-net-5-app-useerrorhandler-a ...

  6. unity项目实现“再按一次退出程序”提示功能

    unity项目,再按一次退出程序,按第一次做提示,再按一次,程序退出. float _waitTime = 2f;//前后两次按退出间隔时间 void OnGUI() { ) { GUI.Label( ...

  7. SOLVED: GATT callback fails to register

    I finally figured this problem out. The device I am using is a Samsung Galaxy S4 and the actual prob ...

  8. SCRUM报告(一)

    我们“来用”团队确定的PM是邓锐.这是我们第一篇SCRUM报告,报告的内容就是我们的Sprint会议.之前冲刺计划会议的内容已发博客,这里简单阐述一下. 一.会议过程大致如下: 1.总结目前的工作进展 ...

  9. Ubuntu实用快捷键

    ALT + TAB: 切换程序窗口Win+w: 显示所有的工作空间,可轻松进行切换 ===== Terminal终端 ===== CTRL + ALT + T: 打开终端 TAB: 自动补全命令或文件 ...

  10. 使用parseJSON代替eval

    有些程序员如果没有很好的在javascript中解析json数据,往往会直接eval把json转成js对象,这时候如果json的数据中包含了被注入的恶意数据,则可能导致代码注入的问题. 正确的做法是分 ...