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 ...
随机推荐
- c语言学习笔记 - 结构体位域
在学习结构体的时候遇到了位域这个概念,位域主要是为了节省内存空间,比如用一个32位,4个字节的int存储一个开关变量时,会造成空间浪费,于是干脆就考虑在这个32划分不同的区域来存储数据,例如划出1位存 ...
- (转)剖析Linux文件编码的查看及修改
Linux文件编码的查看和修改都有不止一种做法,如果你需要在Linux中操作windows下的文件,那么很可能会经常遇到文件编码转换的问题,如何进行这项工作,也应该是经常工作在双系统下的操作者的必须掌 ...
- Glassfish安装、基本使用、在idea中配置Glassfish
Glassfish安装.基本使用. 一.glassfish简介 glassfish是一款web应用服务器,和tomcat一样,也是一款优秀的Servlet容器. 二.glassfish知识点 1.do ...
- CF #575 Div3
// 比赛链接:https://codeforces.com/contest/1196 // CF 2019.7.24 // 本想Div3手速场上分,结果卡在C题,掉了不少分. // 自闭了这么久,今 ...
- mybatis和java一些知识记录
<where> <if test="userName != null and userName != ''"> and user_name like con ...
- 使用Log4net把日志写入到SqlServer数据库
1.官网URL: http://logging.apache.org/log4net/ 2.配置文件参照URL: http://logging.apache.org/log4net/release/c ...
- 汇编语言LAHF和SAHF指令
LAHF(加载状态标志位到 AH)指令将 EFLAGS 寄存器的低字节复制到 AH.被复制的标志位包括:符号标志位.零标志位.辅助进位标志位.奇偶标志位和进位标志位.使用这条指令,可以方便地把标志位副 ...
- Python学习之enumerate
enumerate还可以接收第二个参数,用于指定索引起始值 2. 注意open返回文件对象,可迭代,而os.open返回的是文件指针,int类型, <wiz_tmp_tag ...
- phalcon常用语法
打印SQL //在config/service.php中注册服务 $di->set( 'profiler', function () { return new \Phalcon\Db\Profi ...
- 此处有加速 apt-get github docker pull
ubuntu get-apt 加速 创建 aptupdate.sh 脚本,内容为: #!/bin/bash mv /etc/apt/sources.list /etc/apt/sources.list ...