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. Creating a web application.

    About creating web GIS applications As you learn and use ArcGIS for Server, you'll probably reach th ...

  2. 使用charles proxy for Mac来抓取手机App的网络包

    之前做Web项目的时候,经常会使用Fiddler(Windows下).Charles Proxy(Mac下)来抓包,调试一些东西:现在搞Android App开发,有时候也需要分析手机App的网络请求 ...

  3. cas的url中去掉jsessionid

    Servlet3.0规范中的<tracking-mode>允许你定义JSESSIONID是存储在cookie中还是URL参数中.如果会话ID存储在URL中,那么它可能会被无意的存储 在多个 ...

  4. hdoj 1089(费马小定理)

    题目大意:方程f(x)=5*x^13+13*x^5+k*a*x:输入任意一个数k,是否存在一个数a,对任意x都能使得f(x)能被65整出. 现假设存在这个数a ,因为对于任意x方程都成立 所以,当x= ...

  5. html 标签的嵌套规则

    1. 块元素可以包含内联元素或某些块元素,但内联元素却不能包含块元素,它只能包含其它的内联元素: <div><h1></h1><p></p> ...

  6. 用VS2005写一个 C 的类库和用 C#来调用的示例

    一.用VS2005写一个 C 的类库的步骤: (1).建立一个空的Visual C++项目 (2).这时候在项目中可以看见 三个空目录 选中 "源文件" 目录,然后点鼠标右键,在弹 ...

  7. 利用sfntly的sfnttool.jar提取中文字体

    雨忆博客中提到了sfntly(具体介绍可以看:https://code.google.com/p/sfntly/),利用其中sfnttool.jar就可以提取只包含指定字符的字体,如果想在页面中通过@ ...

  8. xamp配置多域名站点

    xampp配置多站点出现,htdocs目录和虚拟目录二者只能选其一的情况,我的xampp安装在D:\xampp\,默认web根目录在D:\xampp\htdocs,然后我在D:\magento安装了m ...

  9. C、C++、Java异或运算交换变量变量值的区别

    今天看到一位大神的博客,深受感触.决定也发一篇博客,证明一下我还活着. 于是我翻看以前学习时做的一些笔记,整理了一下,得到了一个关于异或运算交换变量变量值的笔记. 首先来看下面三组表达式,看起来他们都 ...

  10. linux下gdal的python包的安装

    由于python包是从C++包编译出来的,所以需要先下载源码进行编译安装.1. gdal下载http://download.osgeo.org/gdal/CURRENT/sudo ./configur ...