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列表里。

  1. public static List<Interval> merge(List<Interval> it) {
  2. List<Interval> res = new ArrayList<>();
  3. if (it == null || it.size() == 0) {
  4. return res;
  5. }
  6. Comparator<Interval> comp = new Comparator<Interval>() {
  7. @Override
  8. public int compare(Interval o1, Interval o2) {
  9. if (o1.start - o2.start != 0) {
  10. return o1.start - o2.start;
  11. }
  12. return o2.end - o1.end;
  13. }
  14. };
  15. Collections.sort(it, comp);
  16. int begin = it.get(0).start;
  17. int end = it.get(0).end;
  18. for (int i = 0; i < it.size() - 1; i++) {
  19. if (it.get(i).end < it.get(i + 1).start) {
  20. res.add(new Interval(begin, it.get(i).end));
  21. begin = it.get(i + 1).start;
  22. } else {
  23. it.get(i + 1).end = Math.max(it.get(i).end, it.get(i + 1).end);
  24. }
  25. end = it.get(i + 1).end;
  26.  
  27. }
  28. res.add(new Interval(begin, end));
  29. return res;
  30. }

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

  1. public static List<Interval> merge(List<Interval> it) {
  2. List<Interval> res = new ArrayList<>();
  3. if (it == null || it.size() == 0) {
  4. return res;
  5. }
  6. //这里如果用lambda表达式,会超时
        
        //Comparator<Interval> comp = (o1, o2) -> o1.start - o2.start;
  1. Comparator<Interval> comp = new Comparator<Interval>() {
  1. @Override
  2. public int compare(Interval o1, Interval o2) {
  3. return o1.start - o2.start;
  4. }
  5. };
  6. Collections.sort(it, comp);
  7. Interval curr;
  8. Iterator<Interval> itor = it.iterator();
  9. curr = itor.next();
  10. while (itor.hasNext()) {
  11. Interval tmp = itor.next();
  12. if (curr.end >= tmp.start) {
  13. curr.end = Math.max(curr.end, tmp.end);
  14. itor.remove();
  15. } else {
  16. curr = tmp;
  17. }
  18. }
  19. return res;
  20. }

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. hadoop集群环境搭建准备工作

    一定要注意hadoop和linux系统的位数一定要相同,就是说如果hadoop是32位的,linux系统也一定要安装32位的. 准备工作: 1 首先在VMware中建立6台虚拟机(配置默认即可).这是 ...

  2. windows下apache+php+mysql配置

    Apache 2.4.10(文件:httpd-2.4.10-win64-VC11.zip) php 5.6.26 (文件:php-5.6.25-Win32-VC11-x64.zip) mysql 5. ...

  3. CSS Clip属性

    Clip属性在W3C官网是这样进行描述的:“通过对元素进行剪切来控制元素的可显示区域,默认情况下,元素是不进行任何剪切的,但是也有可能剪切区域也显示的设置了clip属性”. .selector { c ...

  4. 在企业级开发中使用Try...Catch...会影响效率吗?

    感谢神啊.上帝及老天爷让我失眠,才能够有了本篇文章. 记得不久之前,公司一同事曾经说过:“如果是Winform开发,由于程序是在本地,使用try...catch不会有太大性能问题,可是如果是在web服 ...

  5. mysqldump备份、还原数据库路径名含有空格的处理方法(如:Program Files)

    虽然以下的方法也可以解决,不过最简单直接的,还是直接在路径前后加双引号-" ",这个方法简单有效. 首先要说明的是mysqldump.exe在哪里不重要,重要的是要处理好路径中的非 ...

  6. [转]mysql导出导入中文表解决方法

    在开发过程中会经常用到mysql导出导入中文表,本文将详细介绍其如何使用,需要的朋友可以参考下. 在开发过程中会经常用到mysql导出导入中文表,本文将详细介绍其如何使用,需要的朋友可以参考下一.先针 ...

  7. windows API 统计系统字体

    最近工作中遇到一个需求,需要统计当前系统中包含的所有字体.在网上逛了一圈后发现了EnumFontFamiliesEx这个API好像就可以实现这个功能.这里将自己对这个API的理解做一个记录,算是对这块 ...

  8. Qt Quick 与 QML语言(初学笔记1)

    Qt Quick Qt Quick是一些新的UI技术的集合,用来帮助开发者创建一种现在越来越多用于手机.多媒体播放器.机顶盒以及其他便携式设备上的直观的.现代的.流畅的用户界面.简单来说,Qt Qui ...

  9. IOS下双击背景, touchmove, 阻止页面背景scroll.

    ios prevent dblclick(tap) page scrollhtml add:("minimal-ui" is very important) <meta na ...

  10. Jquery异步请求数据实例

    一.Jquery向aspx页面请求数据 前台页面JS代码: $("#Button1").bind("click", function () { $.ajax({ ...