题目链接

Merge Intervals - LeetCode

注意点

  • 区间是无序的
  • 每个区间start一定小于end

解法

解法一:首先以start的值从小到大来排序,排完序我们就可以开始合并了。先把第一个区间存入ret,然后从第二个开始遍历所有区间,如果与ret中最后一个区间有重叠就更新,否则就直接存入ret。时间复杂度O(n)

/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class Solution {
public:
vector<Interval> merge(vector<Interval>& intervals) {
if (intervals.empty()) return {};
sort(intervals.begin(), intervals.end(), [](Interval &a, Interval &b) {return a.start < b.start;});
vector<Interval> ret{intervals[0]};
int i,n = intervals.size();
for(i = 1;i < n;i++)
{
if(ret.back().end < intervals[i].start) ret.push_back(intervals[i]);
else ret.back().end = max(ret.back().end,intervals[i].end);
}
return ret;
}
};

小结

Merge Intervals - LeetCode的更多相关文章

  1. 56. Merge Intervals - LeetCode

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

  2. Merge Intervals——LeetCode

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  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. netbeans 类重复 解决

    Help -> About -> Cache directory 记录Cache directory目录 删除该目录下的所有文件 重启

  2. opengl绘制三角形

    顶点数组对象:Vertex Array Object,VAO 顶点缓冲对象:Vertex Buffer Object,VBO 索引缓冲对象:Element Buffer Object,EBO或Inde ...

  3. python装饰器(披着羊皮的狼)

    python装饰器的作用是在不改变原有函数的基础上,对函数的功能进行增加或者修改. 装饰器语法是python语言更加优美且避免很多繁琐的事情,flask中配置路由的方式便是装饰器. 首先python中 ...

  4. django1.11入门

    快速安装指南¶ 在使用Django之前,您需要安装它.我们有 完整的安装指南,涵盖所有可能性; 本指南将指导您进行简单,最小化的安装,在您完成介绍时可以正常工作. 安装Python¶ 作为一个Pyth ...

  5. 对PBFT算法的理解

    PBFT论文断断续续读了几遍,每次读或多或少都会有新的理解,结合最近的项目代码,对于共识的原理有了更清晰的认识.虽然之前写过一篇整理PBFT论文的博客,但是当时只是知道了怎么做,却不理解为什么.现在整 ...

  6. 8.openldap mirrormode(主主同步)

    作者:yaoyao #MirrorMode双主模式 1.主机: ldap01.liuyao.com ldap02.liuyao.com 2.搭建LDAP服务 搭建过程省略,保证2台服务器部署配置一样即 ...

  7. unset命令详解

    基础命令学习目录首页 功能说明:unset是一个内建的Unix shell命令,在Bourne shell家族(sh.ksh.bash等)和C shell家族(csh.tcsh等)都有实现.它可以取消 ...

  8. fetch上传文件

    通过简单的配置,实现form表单文件上传 var formData = new FormData(); var fileField = document.querySelector("inp ...

  9. Python参数传递,既不是传值也不是传引用

    面试的时候,有没有被问到Python传参是传引用还是传值这种问题?有没有听到过Python传参既不是传值也不是传引用这种说法?一个小小的参数默认值也可能让代码出现难以查找的bug? 如果你也遇到过上面 ...

  10. Python20-Day05

    一.模块与包 1.模块 什么是模块? 在python中,模块可以分为四个通用类别: 1. 使用python编写的.py文件 2. 已经被编译为共享库或DLL的c或者c++扩展 3. 把一系列模块组织到 ...