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. 利用DIV,实现简单的网页布局

    <html lang="en"><head> <meta charset="UTF-8"> <title>GIS ...

  2. mysql数据库优化日志(更)-howyue

    1)记一次首页查询优化 优化前: 优化后: 主要优化: 1.select查询只查询需要字段: 2.where条件字段添加索引:

  3. jquery,javascript -设置某一ul下的li下的 a的属性

    //javascriptvar ul = document.getElementById('ul); var as = ul.getElementsByTagName('a'); for(var i ...

  4. asp.net mvc4 使用java异步提交form表单时出现[object object] has no method ajaxSubmit

    最近接手了一个单子,说大不大,只是功能不少,开发过程中遇到该问题 先看脚本截图: 本以为是笔误,哪儿写错了,可是看来看去,都没发现有不合适的地方,对比过网上很多代码,都差不多,于是各种方式的,各种原因 ...

  5. 把cygwin加入右键菜单

    第一步:修改windows注册表 1·开始->运行(或者win键+R),输入REGEDIT,回车,打开注册表编辑器: 2·找到HKEY_CLASSES_ROOT\Directory\Backgr ...

  6. iOS6以后的单个控制器横竖屏显示以及旋转屏控制技巧,附带iOS8以后显示电池状态栏

    一.在应用中从竖屏模式强制转换为横屏模式 第一种方法:通过模态弹出视图的方式,使得特定ViewController坚持特定的interfaceOrientation(1)iOS6之后提供了这样一个方法 ...

  7. extern "C" {} 来沟通C和C++

    比如说你用C++开发了一个DLL库,为了能够让C语言也能够调用你的DLL输出(Export)的函数,你需要用extern "C"来强制编译器不要修改你的函数名. 通常,在C语言的头 ...

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

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

  9. Windows Open with Sublime Text

    Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\*\shell\Open with Sublime Text] "Icon&q ...

  10. O-C浮点数转化整数

    1.简单粗暴,直接转化 float f = 1.5; int a; a = (int)f; NSLog("a = %d",a); 输出结果是1.(int)是强制类型转化,丢弃浮点数 ...