题目链接

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. 粒子群算法(PSO)关于参数w的一些改进方法

    (一)线性递减 function [xm,fv] = PSO_lin(fitness,N,c1,c2,wmax,wmin,M,D) format long; % fitness学习函数 % c1学习因 ...

  2. NO--15 微信小程序,scroll-view选项卡和跳转

    大多数的商城类小程序都有这个功能,点击“全部订单”,“待付款”,“待发货”,“待收货”,“已完成”,会跳转页面且跳至与之相对应的选项卡中.所以我们在开发该小程序时也做了相同的功能.如下图:   scr ...

  3. JavaScript学习笔记(八)—— 补

    第九章 最后的补充 一.Jquery简单阐述 JQuery是一个JavaScript库,旨在减少和简化处理DOM和添加视觉效果的JavaScript代码:使用时必须得添加库路径:学习路径:http:/ ...

  4. 7行Python代码的人脸识别

    随着去年alphago 的震撼表现,AI 再次成为科技公司的宠儿.AI涉及的领域众多,图像识别中的人脸识别是其中一个有趣的分支.百度的BFR,Face++的开放平台,汉王,讯飞等等都提供了人脸识别的A ...

  5. OGG 跳过事务(转)

    http://blog.chinaunix.net/uid-26190993-id-3434074.html    在OGG运行过程中,通常会因为各种各样的原因导致容灾端的REPLICAT进程ABEN ...

  6. 查看jdk使用的是什么垃圾收集器

    一.方法一 打印虚拟机所有参数 [root@localhost ~]# java -XX:+PrintFlagsFinal -version | grep :    uintx InitialHeap ...

  7. HTML常用头部变量

    简例:访问baidu的头部 GET /?tn=98827400_hao_pg HTTP/1.1 Host: www.baidu.com Connection: keep-alive Cache-Con ...

  8. MathExam第二次作业

    第二次作业:MathExam 一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟) Planning 计划 20 30 • ...

  9. “Gogoing”改进方案

    通过看见他们对我们团队的意见点评,我们还要有更多改善的地方. 首先,就是界面的优化: 其次,加上自己些特有的功能,吸引更多的用户: 然后,需要整理大量的数据库信息才能完善: 最后,需要有其他软件的集成 ...

  10. 谈对“Git”的认识与理解

    自诞生于2005年以来,Git日臻完善,在高度易用的同时,仍然保留着初期设定的目标.它的速度飞快,及其适合管理大项目,它还有着令人难以置信的非线性分支管理系统,可以应付各种复杂的项目开发需求.接着说说 ...