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 ...
随机推荐
- OpenGL(2)-窗口
写在前面 通过本节,你可以毫不费力的--->创建一个窗口 OpenGL中窗口,即载体 导入头文件 #include <glad/glad.h> #include <GLFW/g ...
- Robot的使用
在Java中,有一个类,非常神奇,它能帮助你完成某些任务,例如:打开笔记本/QQ等. 今天,我就说一下Robot类的使用方法吧,做一个打开记事本的小程序. 1.准备工作 JDK:不知道的别看了 开发工 ...
- centos7.6 安装配置rabbitmq
IP地址:192.168.200.108 安装erlang 和 依赖环境 yum install -y socat yum install -y erlang 安装rabbitmq yum insta ...
- [Hanani]高数相关知识记录
分部积分 \(\int uv'{\rm d}x=uv-\int u'v{\rm d}x\)
- js备忘录1
新建对象 赋值和取值操作 var book={ topic:"JavaScript", fat: true }; book.topic 通过点访问 book["fat& ...
- iOS软件"一天八杯水“app开发过程
作为一个ios系统测试者和app外观设计者.我们首先要了解iOS系统的开发工具和资源.xcode和iOS sdk作为一个免费的开发环境值得我们去学习和了解.interface builder提供创建了 ...
- Servlet 3.0对上传的支持
Servlet 2.5 进行上传 首先对表单的要求 ->method ="post" ->enctype="multipart/form-d ...
- Beta Scrum Day 6 — 听说
听说
- BNUOJ 52317 As Easy As Possible 树上倍增/主席树
题目链接: https://acm.bnu.edu.cn/v3/problem_show.php?pid=52317 As Easy As Possible Case Time Limit: 1000 ...
- week3a:个人博客作业
1.博客上的问题 阅读下面程序,请回答如下问题: using System; using System.Collections.Generic; using System.Text; namespac ...