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

题目大意:给定一个组区间,实现一个函数,合并这些区间。

解题思路:首先根据start排序,然后开始循环,判断当前end和下一个的start是否有overlap,没有的话,可以输出当前这个区间,有的话把后一个的end设置为max{后一个end,当前end},循环到最后,把所有符合条件的都加到res列表里。

    public static List<Interval> merge(List<Interval> it) {
List<Interval> res = new ArrayList<>();
if (it == null || it.size() == 0) {
return res;
}
Comparator<Interval> comp = new Comparator<Interval>() {
@Override
public int compare(Interval o1, Interval o2) {
if (o1.start - o2.start != 0) {
return o1.start - o2.start;
}
return o2.end - o1.end;
}
};
Collections.sort(it, comp);
int begin = it.get(0).start;
int end = it.get(0).end;
for (int i = 0; i < it.size() - 1; i++) {
if (it.get(i).end < it.get(i + 1).start) {
res.add(new Interval(begin, it.get(i).end));
begin = it.get(i + 1).start;
} else {
it.get(i + 1).end = Math.max(it.get(i).end, it.get(i + 1).end);
}
end = it.get(i + 1).end; }
res.add(new Interval(begin, end));
return res;
}

看到别人还有更高效的方案,直接遍历源List,在上面操作,不需要new新的Interval,本来想用Java8的lambada表达式写Comparator的,但是不知道为什么在提交的时候总是TLE。

public static List<Interval> merge(List<Interval> it) {
List<Interval> res = new ArrayList<>();
if (it == null || it.size() == 0) {
return res;
}
//这里如果用lambda表达式,会超时
    
    //Comparator<Interval> comp = (o1, o2) -> o1.start - o2.start;
        Comparator<Interval> comp = new Comparator<Interval>() {
            @Override
public int compare(Interval o1, Interval o2) {
return o1.start - o2.start;
}
};
Collections.sort(it, comp);
Interval curr;
Iterator<Interval> itor = it.iterator();
curr = itor.next();
while (itor.hasNext()) {
Interval tmp = itor.next();
if (curr.end >= tmp.start) {
curr.end = Math.max(curr.end, tmp.end);
itor.remove();
} else {
curr = tmp;
}
}
return res;
}

Merge Intervals——LeetCode的更多相关文章

  1. Merge Intervals - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Merge Intervals - LeetCode 注意点 区间是无序的 每个区间start一定小于end 解法 解法一:首先以start的值从小到大来 ...

  2. 56. Merge Intervals - LeetCode

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

  3. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

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

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

  5. LeetCode: Merge Intervals 解题报告

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

  6. [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals

    Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...

  7. [Leetcode Week2]Merge Intervals

    Merge Intervals题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/merge-intervals/description/ Descript ...

  8. 【LeetCode】Merge Intervals 题解 利用Comparator进行排序

    题目链接Merge Intervals /** * Definition for an interval. * public class Interval { * int start; * int e ...

  9. 【leetcode】Merge Intervals

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

随机推荐

  1. Visual Studio中Js使用智能感知

    使用了第三方的JS库或框架,在VS中编写JS代码,发现真是个悲剧,完全只能手打,智能感知没了,这不符合VS的一贯做风只要在写代码的JS文件加上以下代码,就可以有智能感知了 ///<referen ...

  2. Length 和 Width在矩形中的定义.

    Length is the longer or longest dimension of a rectangle (or even an object). Ref:http://mathforum.o ...

  3. (whh仅供自己参考)进行ip网络请求的步骤

    这个过程大致是这个样子: 1 添加通知 2 发送网络请求 里边有一个发送通知的操作 3 执行发送通知的具体操作 代码如下: 1 在VC添加通知 [[NSNotificationCenter defau ...

  4. Codeforces Round #287 D.The Maths Lecture

    The Maths Lecture 题意:求存在后缀Si mod k =0,的n位数的数目.(n <=1000,k<=100); 用f[i][j]代表 长为i位,模k等于j的数的个数. 可 ...

  5. Java 可变参数

    java1.5增加了新特性:可变参数:适用于参数个数不确定,类型确定的情况,java把可变参数当做数组处理.注意:可变参数必须位于最后一项.当可变参数个数多余一个时,必将有一个不是最后一项,所以只支持 ...

  6. phpexcel 一些基本的设置 (表格的基本属性)

    网址是:http://www.thinkphp.cn/code/1893.html

  7. php数组基础

    一.php数组的声明      1.数组中可以有任意类型的数据      2.长度可以变长      3.数组的分类:           a.索引数组:数组是以从0开始的帧数作为索引值       ...

  8. php生成缩略图

    <?php /** * 生成缩略图函数(支持图片格式:gif.jpeg.png和bmp) * @author ruxing.li * @param string $src 源图片路径 * @pa ...

  9. jsonp是什么以及jsonp的使用

    1概述 Jsonp(JSON with Padding)是资料格式 json 的一种“使用模式”,可以让网页从别的网域获取资料.由于同源策略,一般来说位于 server1.example.com 的网 ...

  10. ipconfig的C语言实现

    首先,这篇文章实现了两种方法查询IP,实现截图如下: 第一种方法时调用系统命令,代码如下: #include <cstdlib> #include <iostream> usi ...