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. python 爬虫下载英语听力新闻(npr news)为mp3格式

    想通过听实时新闻来提高英语听力,学了那么多年的英语,不能落下啊,不然白费背了那么多年的单词. npr news是美国国家公共电台,发音纯正,音频每日更新,以美国为主,世界新闻为辅,比如最近我国武汉发生 ...

  2. 最长特殊序列 II

    最长特殊序列 II class Solution { boolean containsSub(String s,String p){ int i,j; for(i=0,j=0;i<p.lengt ...

  3. Opencv中的轮廓(不全)

    1.初识轮廓 为了准确,要使用二值化图像.在寻找轮廓之前,要进行阈值化处理,或者Canny边界检测. 查找轮廓的函数会修改原始图像.如果你在找到轮廓之后还想使用原始图像的话,你应该将原始图像存储到其他 ...

  4. Java简单调用Lua

    package lua; import org.keplerproject.luajava.LuaState; import org.keplerproject.luajava.LuaStateFac ...

  5. 腾讯一shell试题.

    腾讯一shell试题. 假设qq.tel文件内容: 12334:13510014336 12345:12334555666 12334:12343453453 12099:13598989899 12 ...

  6. CSS 选择器权重计算规则(转)

    其实,CSS有自己的优先级计算公式,而不仅仅是行间>内部>外部样式:ID>class>元素. 一.样式类型 1.行间 <h1 style="font-size: ...

  7. HDU-3038 How Many Answers Are Wrong(带权并查集区间合并)

    http://acm.hdu.edu.cn/showproblem.php?pid=3038 大致题意: 有一个区间[0,n],然后会给出你m个区间和,每次给出a,b,v,表示区间[a,b]的区间和为 ...

  8. 素小暖讲JVM:Eclipse运行速度调优

    本系列是用来记录<深入理解Java虚拟机>这本书的读书笔记.方便自己查看,也方便大家查阅. 欲速则不达,欲达则欲速! 这两天看了JVM的内存优化,决定尝试一下,对Eclipse进行内存调优 ...

  9. 实践一次有趣的sql优化

    课程表 #课程表 create table Course( c_id int PRIMARY KEY, name varchar(10) ) 增加 100 条数据 #增加课程表100条数据 DROP ...

  10. 蓝桥杯 传球游戏(dp)

    Description 上体育课的时候,小蛮的老师经常带着同学们一起做游戏.这次,老师带着同学们一起做传球游戏.游戏规则是这样的:n个同学站成一个圆圈,其中的一个同学手里拿着一个球,当老师吹哨子时开始 ...