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 ...
随机推荐
- VS2010-MFC(对话框:模态对话框及其弹出过程)
转自:http://www.jizhuomi.com/software/160.html 一.模态对话框和非模态对话框 Windows对话框分为两类:模态对话框和非模态对话框. 模态对话框是这样的对话 ...
- 关于mybatis对实体类参数绑定参数的问题
dao层的代码: public interface SupplierMapper extends BaseMapper<SupplierDbo>{ /*List<SupplierDb ...
- HDU - 3007 Buried memory
传送门 最小圆覆盖模板. //Achen #include<algorithm> #include<iostream> #include<cstring> #inc ...
- (转)AngularJS判断checkbox/复选框是否选中并实时显示
最近做了一个选择标签的功能,把一些标签展示给用户,用户选择自己喜欢的标签,就类似我们在购物网站看到的那种过滤标签似的: 简单的效果如图所示: 首先看一下html代码: <!DOCTYPE htm ...
- PAT甲级——A1030 Travel Plan
A traveler's map gives the distances between cities along the highways, together with the cost of ea ...
- CentOS 6.5 Apache+SVN使用户可以自己修改密码
yum -y install php #安装php mkdir /var/www/svnadmin #建立页面目录 vi /var/www/svnadmin/index.php #创建index.ph ...
- JSP-案例-商品增删改
商品的增删改查 1显示 部分代码 Dao public List<Product> findAllProduct() throws SQLException { QueryRunner r ...
- storm的安装
一. 安装storm要先本机搭建好zookeeper集群(手动目录安装或者CDH安装) 二. Java 6 Python 2.6.6 unzip(针对使用语言要安装好相应环境 比如java 要JDK和 ...
- OSG能够在当前帧截图,也就是能转换视角后马上截图
#include <Windows.h> #include <osg/GraphicsContext> #include <osg/Group> #include ...
- js校验文本框只能输入数字(包括小数)
form表单 <form method="POST" action=""> <input type="text" id=& ...