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 ...
随机推荐
- Python 3 利用 Dlib 19.7 进行人脸检测
0. 引言 / Overview 介绍 Dlib 中基于 HOG,Histogram of Oriented Gradients / 方向梯度直方图 实现 Face Detect / 人脸检测 的两个 ...
- Netty源码分析第2章(NioEventLoop)---->第4节: NioEventLoop线程的启动
Netty源码分析第二章: NioEventLoop 第四节: NioEventLoop线程的启动 之前的小节我们学习了NioEventLoop的创建以及线程分配器的初始化, 那么NioEvent ...
- .NetCore mvc Ajax Post数据到后端
在前端页面中,如果没有表单,想把复杂对象提交到后端,可使用以下方法 后端Controller中定义以下方法: [HttpPost] public int AddSolution([FromBody]S ...
- VGG——Very deep convolutional networks for large-scale image recognition
1. 摘要 在使用非常小(3×3)的卷积核情况下,作者对逐渐增加网络的深度进行了全面的评估,通过设置网络层数达 16-19 层,最终效果取得了显著提升. 2. 介绍 近来,卷积神经网络在大规模图像识别 ...
- Xavier——Understanding the difficulty of training deep feedforward neural networks
1. 摘要 本文尝试解释为什么在深度的神经网络中随机初始化会让梯度下降表现很差,并且在此基础上来帮助设计更好的算法. 作者发现 sigmoid 函数不适合深度网络,在这种情况下,随机初始化参数会让较深 ...
- 对PBFT算法的理解
PBFT论文断断续续读了几遍,每次读或多或少都会有新的理解,结合最近的项目代码,对于共识的原理有了更清晰的认识.虽然之前写过一篇整理PBFT论文的博客,但是当时只是知道了怎么做,却不理解为什么.现在整 ...
- Linux 系统安全检查(shell)
脚本内容: #!/bin/bash echo " (__)" echo " (oo)" echo " /------\/ " echo &q ...
- last命令详解
基础命令学习目录首页 原文链接:https://blog.csdn.net/jerry_1126/article/details/54427119 [root@xiaoma /root] test!# ...
- 随手记录-linux-vim使用
- Notes of Daily Scrum Meeting(11.13)
Notes of Daily Scrum Meeting(11.13) 今天邹欣老师给我们讲课大家还是很有收获的,大家课堂的参与度确实有了很大的提升,而且邹欣老师关于项目Scrum Meeting报告 ...