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 ...
随机推荐
- DotNetBar2设置窗体为office风格
帮同事写个桌面工具,刚学着用DotNetBar2.后面面分享学习过程.下面是给窗体设置成office风格的方法.(1)拷贝DotNetBar2的dll文件,添加引用. (2)在需要用office风格的 ...
- express 4 使用session和cookies
https://my.oschina.net/u/1466553/blog/294336 http://blog.csdn.net/liyi109030/article/details/3527138 ...
- Linux下c开发 之 线程通信
Linux下c开发 之 线程通信 1.Linux“线程” 进程与线程之间是有区别的,不过Linux内核只提供了轻量进程的支持,未实现线程模型.Linux是一种“多进程单线程”的操作系统.Linux本身 ...
- java.io.FileNotFoundException: E:\work\work (拒绝访问。)
转载自:https://blog.csdn.net/YQS_Love/article/details/51959776 一.问题 在使用FileInputStream或FileOutputStream ...
- flock文件锁的学习和应用
flock文件锁 学习与应用 2016-9-20 作用: 可以使用flock文件锁,避免指定命令的同时执行.(实现任务锁定,解决冲突) 用法: # flock -xn /opt/lock_file ...
- idea查看jar冲突和解决方法
选中Dependencies,点上边那个按钮,出现下图 依赖图太小了,根本没法看啊?好办,点击鼠标右键,呼出右键菜单栏,然后点击Actual Size: 如果我们仔细观察上图,会发现在项目依赖图中,有 ...
- java linkedlist和arraylist添加元素时性能比较
- jdk不同版本的垃圾收集器
- linux学习(三)-----linux用户管理、实用指令
用户管理 基本介绍 说明: 1.Linux 系统是一个多用户多任务的操作系统,任何一个要使用系统资源的用户,都必须首先向 系统管理员申请一个账号,然后以这个账号的身份进入系统. 2.Linux 的用户 ...
- jeecms 前台拦截器的研究与改造
jeecms 前台拦截器的研究与改造 2013年12月24日 15:23:35 xinfei0803 阅读数 3511 jeecms出发点是面向大众的,具有前台开发性,也就是说,即时是未登录(游客 ...