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的更多相关文章
- 56. Merge Intervals - LeetCode
Question 56. Merge Intervals Solution 题目大意: 一个坐标轴,给你n个范围,把重叠的范围合并,返回合并后的坐标对 思路: 先排序,再遍历判断下一个开始是否在上一个 ...
- Merge Intervals——LeetCode
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [Leetcode][Python]56: Merge Intervals
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...
- LeetCode: Merge Intervals 解题报告
Merge IntervalsGiven a collection of intervals, merge all overlapping intervals. For example,Given [ ...
- [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals
Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...
- [Leetcode Week2]Merge Intervals
Merge Intervals题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/merge-intervals/description/ Descript ...
- 【LeetCode】Merge Intervals 题解 利用Comparator进行排序
题目链接Merge Intervals /** * Definition for an interval. * public class Interval { * int start; * int e ...
- 【leetcode】Merge Intervals
Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ...
随机推荐
- SICP读书笔记 3.4
SICP CONCLUSION 让我们举起杯,祝福那些将他们的思想镶嵌在重重括号之间的Lisp程序员 ! 祝我能够突破层层代码,找到住在里计算机的神灵! 目录 1. 构造过程抽象 2. 构造数据抽象 ...
- 离线人脸识别 ArcFaceSharp -- ArcFace 2.0 SDK C#封装库分享
ArcFaceSharp ArcFaceSharp 是ArcSoft 虹软 ArcFace 2.0 SDK 的一个 C# 封装库,为方便进行 C# 开发而封装.欢迎 Start & Fork. ...
- 基于C#的机器学习--贝叶斯定理-执行数据分析解决肇事逃逸之谜
贝叶斯定理-执行数据分析解决肇事逃逸之谜 在这一章中,我们将: 应用著名的贝叶斯定理来解决计算机科学中的一个非常著名的问题. 向您展示如何使用贝叶斯定理和朴素贝叶斯来绘制数据,从真值表中发现异常值 ...
- pytorch 如何使用tensorboard实时查看曲线---- tensorboardX简介
用惯了tensorflow的小伙伴肯定都用过tensorboard工具吧.虽然Facebook也推出了visdom,但是在一次不小心误触clear之后,我放弃了这个工具(页面的一个clear按钮我本来 ...
- Vue+webpack项目中,运行报错Cannot find module 'chalk'的处理
刚开始用vue + webpack新建项目,在github上下载了一个示例,输入npm init >>>npm run dev 后报错 Cannot find module 'cha ...
- [leetcode-921-Minimum Add to Make Parentheses Valid]
Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...
- jenkins配置01--用户添加及权限配置
原文出自:https://www.cnblogs.com/kevingrace/p/6019707.html 下面重点记录下jenkins安装后的一些配置: (1)添加用户权限 jenkins初次登陆 ...
- Notes of Daily Scrum Meeting(11.8)
Notes of Daily Scrum Meeting(11.8) 预备中开始写代码的第一天,因为大家对Android编程的熟悉程度还是不够,所以工程进行的非常缓慢,有四名队员 开始编写自己的任务, ...
- TeamWork#2,Week 2,We are sixsix!
We are sixsix! (从左至右依次是:郝倩.张志浩.高雅智[高哥].牛强.张明培育.彭林江.王卓) 郝倩,来自120617班,我们组7个成员中唯一一个6行政班以外的成员.为了达成组队条件,彭 ...
- css3学习笔记一
首先界面是二维的但也可以有三维的效果.先了解浏览器兼容性问题,火狐加前缀(-moz-)IE加(-MF-)谷歌加(-webkit),简单介绍css3的几个属性. 对于背景来说如果是单纯着一种颜色可以会单 ...