Given a collection of intervals, merge all overlapping intervals.

Example 1:

Input: [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].

Example 2:

Input: [[1,4],[4,5]]
Output: [[1,5]]
Explanation: Intervals [1,4] and [4,5] are considered overlapping.

NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.

Solution 1:

class Solution {
public int[][] merge(int[][] intervals) {
List<int[]> res = new ArrayList<>();
if (intervals.length == 0) {
return new int[][] {};
}
Arrays.sort(intervals, (a, b) -> (a[0] - b[0]));
int start = intervals[0][0];
int end = intervals[0][1];
for (int[] interval: intervals) {
if (interval[0] <= end) {
end = Math.max(end, interval[1]);
} else {
res.add(new int[]{start, end});
start = interval[0];
end = interval[1];
}
}
// need to add back the last tuple
res.add(new int[]{start, end});
return res.toArray(new int[][] {});
}
}
class Solution {
public int[][] merge(int[][] intervals) {
if (intervals == null || intervals.length == 0 || intervals[0].length == 0) {
return intervals;
}
List<int[]> list = new ArrayList<>();
int[] startArr = new int[intervals.length];
int[] endArr = new int[intervals.length];
for (int i = 0; i < intervals.length; i++) {
startArr[i] = intervals[i][0];
endArr[i] = intervals[i][1];
}
Arrays.sort(startArr);
Arrays.sort(endArr);
int start = startArr[0];
int end = endArr[0];
for (int i = 1; i < intervals.length; i++) {
if (startArr[i] <= end) {
end = endArr[i];
} else {
list.add(new int[]{start, end});
start = startArr[i];
end = endArr[i];
}
}
list.add(new int[]{start, end});
int[][] res = new int[list.size()][2];
int count = 0;
for (int i = 0; i < list.size(); i++) {
res[i][0] = list.get(i)[0];
res[i][1] = list.get(i)[1];
}
return res;
}
}

Solution 2:

/**
* Definition of Interval:
* public class Interval {
* int start, end;
* Interval(int start, int end) {
* this.start = start;
* this.end = end;
* }
* }
*/ public class Solution {
/**
* @param intervals: interval list.
* @return: A new interval list.
*/
public List<Interval> merge(List<Interval> intervals) {
// write your code here
if (intervals == null || intervals.size() <= 1) {
return intervals;
}
List<Interval> res = new ArrayList<>();
// Collections work on List while Arrays work on array
Collections.sort(intervals, new Comparator<Interval>() {
@Override
public int compare(Interval a, Interval b) {
return a.start - b.start;
}
}); Interval pre = null;
for (Interval cur: intervals) {
if (pre == null || cur.start > pre.end) {
res.add(cur);
pre = cur;
} else {
pre.end = Math.max(cur.end, pre.end);
}
}
return res;
}
}

[LC] 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. 56. Merge Intervals

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

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

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

随机推荐

  1. 使用MySQL传输表空间迁移数据

    对于大表的迁移,如果使用mysqldump进行导出,然后重新导入到其它环境,速度是非常缓慢的.如果使用传输表空间,则可以解决这个问题. 测试使用传输表空间迁移表,基础环境如下:   源库 目标库 IP ...

  2. 201503-1 图像旋转 Java

    思路: 观察输入和输出可以发现,第三列输出为第一行,第二列输出为第二行,第一列输出为第三行.循环即可 import java.util.Scanner; //得分80,本题最高需要输入100W次,因为 ...

  3. 吴裕雄--天生自然 JAVA开发学习: 循环结构

    public class Test { public static void main(String args[]) { int x = 10; while( x < 20 ) { System ...

  4. Django-rest framework框架的三大认证组件

    源码分析:三大认证组件的封装 组件的认证配置: 模型层:models.py class User(BaseModel): username = models.CharField(verbose_nam ...

  5. 关于nginx配置的一个报错connect() to unix:/tmp/php-cgi.sock failed (2: No such file or directory)

    针对配置php的情况: linux服务器一般提示这个 connect() to unix:/tmp/php-cgi.sock failed (2: No such file or directory) ...

  6. 基于Linux下的C语言项目实战--本地账号管理系统

    C语言开发项目实战: C语言是一门通用计算机编程语言,广泛应用于底层开发.C语言的设计目标是提供一种能以简易的方式编译.处理低级存储器.产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言.尽 ...

  7. webfrom 控件

    服务器基本控件: button: text属性 linkbutton:text属性,它是一个超链接模样的普通button hyperlink: navigateurl:链接地址,相当于<a> ...

  8. elasticsearch min_hash 应用分析

    需求作相似文本查询 爬虫作页面去重,会用到simhash,第一个想到的是用simhash算法 但在现有数据集(elasticsearch集群)上用simhash,成本高,simhash值还好计算,不论 ...

  9. linux下 c语言调用c++

    /*****************************g++编译cpp 文件为库文件.编译C文件时gcc 要链接 -l stdc++ 这个库**(非常重要)*///定义c++ class 头文件 ...

  10. python tricks 01

    01: 考察range/sort/lambda 对以下数据进行排序 原数据: [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5] 目标数据: [0, -1, 1, -2, 2 ...