Leetcode56. Merge Intervals合并区间
给出一个区间的集合,请合并所有重叠的区间。
示例 1:
输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: 区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].
示例 2:
输入: [[1,4],[4,5]] 输出: [[1,5]] 解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。
struct Interval {
int start;
int end;
Interval() : start(0), end(0) {}
Interval(int s, int e) : start(s), end(e) {}
};
bool cmp2(Interval x, Interval y)
{
if(x.start != y.start)
return x.start < y.start;
else
return x.end < y.end;
}
class Solution {
public:
vector<Interval> merge(vector<Interval>& intervals)
{
vector<Interval> res;
int len = intervals.size();
if(len == 0)
return res;
sort(intervals.begin(), intervals.end(), cmp2);
res.push_back(intervals[0]);
int cnt = 0;
for(int i = 1; i < len; i++)
{
if(intervals[i].start <= res[cnt].end)
{
if(intervals[i].end <= res[cnt].end)
continue;
else
{
res[cnt].end = intervals[i].end;
}
}
else
{
res.push_back(intervals[i]);
cnt++;
}
}
return res;
}
};
Leetcode56. Merge Intervals合并区间的更多相关文章
- [LeetCode] Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...
- [LeetCode] 56 - Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 056 Merge Intervals 合并区间
给出一个区间的集合, 请合并所有重叠的区间.示例:给出 [1,3],[2,6],[8,10],[15,18],返回 [1,6],[8,10],[15,18].详见:https://leetcode.c ...
- LeetCode 56. Merge Intervals 合并区间 (C++/Java)
题目: Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6] ...
- 【LeetCode每天一题】Merge Intervals(合并区间)
Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...
- [array] leetcode-56. Merge Intervals - Medium
leetcode-56. Merge Intervals - Medium descrition Given a collection of intervals, merge all overlapp ...
- 合并区间 · Merge Intervals & 插入区间 · Insert Interval
[抄题]: 给出若干闭合区间,合并所有重叠的部分. 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8, 10] ...
- [leetcode]56. Merge Intervals归并区间
Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...
- leetcode56. Merge Intervals
题目要求: Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6 ...
随机推荐
- IOS 检测摇晃 几个问题
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://kyoworkios.blog.51cto.com/878347/1152692 ...
- 网络工程师课程---3、IP与路由器(ip地址的主要作用是什么)
网络工程师课程---3.IP与路由器(ip地址的主要作用是什么) 一.总结 一句话总结: 用来标识一个节点的网络地址 划分网段 1.如何得到ip地址的网段号? ip和子网掩码 化成二进制后取 与运算 ...
- 深入浅出 Java Concurrency (34): 线程池 part 7 线程池的实现及原理 (2)[转]
线程池任务执行流程 我们从一个API开始接触Executor是如何处理任务队列的. java.util.concurrent.Executor.execute(Runnable) Executes t ...
- Java-Shiro:目录
ylbtech-Java-Shiro:目录 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http://yl ...
- 报错C1189 #error: "No Target Architecture"
根本原因: 是因为单独包含了一些windows.h已经包含了的头文件如"fileapi.h","WinUser.h",但是却没有包含windows.h 或者先包 ...
- matlab调用keras深度学习模型(环境搭建)
matlab没有直接调用tensorflow模型的接口,但是有调用keras模型的接口,而keras又是tensorflow的高级封装版本,所以就研究一下这个……可以将model-based方法和le ...
- Netty ByteBuf泄露定位修改。
1. ByteBuf 2. 问题描述 日志记录中报堆外内存溢出. 3. 问题定位及修改 Netty提供了ByteBuf泄露的检测机制. JVM启动参数中添加: -Dio.netty.leakDetec ...
- js 倒计时毫秒级别显示
<html> <head> <style> div{ width:100%; text-align:center; font-size: 14px; } </ ...
- 初识splay
这东西都没什么板子着实让我很难受啊,只能到网上抄抄补补, 记下两个用到的博客 https://blog.csdn.net/clove_unique/article/details/50630280 h ...
- LUOGU P2296 寻找道路 (noip 2014)
传送门 解题思路 首先建一张反图,从终点dfs出哪个点直接或间接相连,然后直接跑最短路,跑的时候判断一下所连的点是否与终点相连. 代码 #include<iostream> #includ ...