[LeetCode] 56 - Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals.
For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].
思路:
我们首先要做的就是给区间集排序,由于我们要排序的是个结构体,所以我们要定义自己的comparator,才能用sort来排序,我们以start的值从小到大来排序,排完序我们就可以开始合并了,首先把第一个区间存入结果中,然后从第二个开始遍历区间集,如果结果中最后一个区间和遍历的当前区间无重叠,直接将当前区间存入结果中,如果有重叠,将结果中最后一个区间的end值更新为结果中最后一个区间的end和当前end值之中的较大值,然后继续遍历区间集,以此类推可以得到最终结果,代码如下:
/**
* 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:
static bool comp( const Interval &a, const Interval &b)
{
return (a.start < b.start);
} // 自己定义一个comp函数(必须为静态函数),用来对 区间的开始值 进行排序
vector<Interval> merge(vector<Interval>& intervals)
{
// start typing your code below
// if intervals are sorted, it would be easy
vector<Interval> res; // 存放结果区间
if (intervals.empty()) return res; //去掉这句不能AC
sort(intervals.begin(), intervals.end(),comp);//sort函数的用法
res.push_back(intervals[]);// 将第一个区间存放进结果中
for(int i = ; i < intervals.size(); ++i)
{
if (res.back().end >= intervals[i].start)
res.back().end = max(res.back().end, intervals[i].end);// 如果区间有重叠则进行区间融合
else
res.push_back(intervals[i]); // 否则将区间直接存入结果
}
return res;
}
};
注意:
其中一个很重要的知识是sort函数的使用(详情参考C++primer 10.3 定制操作)
sort(words.begin(), words.end(), isShorter)
第三个参数是一个谓词,可以自己定义这个函数,比如上边是对 结构体里的start 元素作比较 进行排序。
[LeetCode] 56 - Merge Intervals 合并区间的更多相关文章
- LeetCode 56. Merge Intervals 合并区间 (C++/Java)
题目: Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6] ...
- [leetcode]56. Merge Intervals归并区间
Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...
- leetcode 56. Merge Intervals 、57. Insert Interval
56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...
- LeetCode 56. Merge Intervals (合并区间)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- [LeetCode] Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...
- 【LeetCode每天一题】Merge Intervals(合并区间)
Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...
- 056 Merge Intervals 合并区间
给出一个区间的集合, 请合并所有重叠的区间.示例:给出 [1,3],[2,6],[8,10],[15,18],返回 [1,6],[8,10],[15,18].详见:https://leetcode.c ...
- LeetCode: 56. Merge Intervals(Medium)
1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域 ...
- Leetcode56. Merge Intervals合并区间
给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: 区间 [1,3] ...
随机推荐
- [20180819]关于父子游标问题(11g).txt
[20180819]关于父子游标问题(11g).txt --//sql语句存在父子游标,子游标堆6在父游标堆0里面.--//如果存在许多子游标的情况下,父游标堆0是否大小是发生变化呢.测试看看.--/ ...
- 光盘yum源搭建
先修改基本的yum原源,使其不生效 cd /etc/yum.repos.d/ mv CentOS-Base.repo CentOS-Base.repo.bak 在修改媒介yum源使其生效 检验是否生效 ...
- python数据类型分类以及运算类型
一.python数据类型 目录: 1.数字(整数.小数) 2.字符串(单引号.双引号.三引号) 3.元组 #元素确定之后不能修改 4.列表 #元素可以修改 5.集合 #不讲顺序,得到的结果没有重复元 ...
- Linux中shell和子shell一点点理解
Linux执行脚本有两种方式,主要区别在于是否建立子shell 1.像sh,bash,./命令是用来执行shell脚本的,在bash/sh命令下,脚本文件可以无"执行权限",即 ...
- NGINX+PHP+ZABBIX,推荐
https://www.cnblogs.com/liuzhennan/articles/5319280.html
- UVA10603-Fill(BFS)
Problem UVA10603-Fill Accept:1162 Submit:10693 Time Limit: 3000 mSec Problem Description There are ...
- BAT美团滴滴java面试大纲(带答案版)之四:多线程Lock
每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code 这是多线程的第二篇. 多线程就像武学中对的吸星大法,理解透了用好了可以得道成仙,俯瞰芸 ...
- .Net使用Redis详解之ServiceStack.Redis(七) 转载https://www.cnblogs.com/knowledgesea/p/5032101.html
.Net使用Redis详解之ServiceStack.Redis(七) 序言 本篇从.Net如何接入Reis开始,直至.Net对Redis的各种操作,为了方便学习与做为文档的查看,我做一遍注释展现 ...
- undo丢失恢复异常恢复,运维DBA反映Oracle数据库无法启动报错ORA-01157 ORA-01110,分析原因为Oracle数据库坏块导致
本文转自 惜纷飞 大师. 模拟基表事务未提交数据库crash,undo丢失恢复异常恢复,运维DBA反映Oracle数据库无法启动报错ORA-01157 ORA-01110,分析原因为Oracle数据库 ...
- 【转】git-stash用法小结
https://www.cnblogs.com/tocy/p/git-stash-reference.html 缘起 今天在看一个bug,之前一个分支的版本是正常的,在新的分支上上加了很多日志没找到原 ...