[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].
问题:给定一个区间集合,整合所有重叠的区间。
对区间集合按照 start 来排序,然后根据 intervals[i].start 和 res.lastElement.end 来整合即可。
int static comp(Interval t1, Interval t2){
int res = (t2.start > t1.start);
return res;
}
vector<Interval> merge(vector<Interval>& intervals) {
vector<Interval> vt;
if (intervals.size() == ) {
return vt;
}
std::sort(intervals.begin(), intervals.end(), comp);
vt.push_back(intervals[]);
for (int i = ; i < intervals.size(); i++) {
if (intervals[i].start <= vt.back().end) {
vt.back().end = max( vt.back().end, intervals[i].end);
}else{
vt.push_back(intervals[i]);
}
}
return vt;
}
[LeetCode] 56. Merge Intervals 解题思路的更多相关文章
- leetcode 56. Merge Intervals 、57. Insert Interval
56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...
- LeetCode: 56. Merge Intervals(Medium)
1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域 ...
- 【LeetCode】56. Merge Intervals 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode 56. Merge Intervals (合并区间)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- [LeetCode] 56 - Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- [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
原题地址 排序+合并,没啥好说的 第一次尝试C++的lambda表达式,有种写js的感觉,很神奇 c11就支持了lambda表达式,仔细想想,我学C++大概就是在09~10年,c11还没有发布,不得不 ...
- [LeetCode] 56. Merge Intervals(vector sort)
/** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0 ...
- LeetCode 56. Merge Intervals 合并区间 (C++/Java)
题目: Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6] ...
随机推荐
- 安装LVS安装LVS和配置LVS的工作比较繁杂
安装LVS安装LVS和配置LVS的工作比较繁杂,读者在配置的过程中需要非常细心和耐心.在本节我们将对其进行详细地介绍.主要包括如下几个核心步骤:1.获取支持LVS的内核源代码如果读者需要使用LVS,需 ...
- ios开发应用内实现多语言自由切换
需求描述:应用内部有一按钮,点击切换语言(如中英文切换).说起来这个是好久以前做的一个功能点了,刚开始也是没有头绪,后来解决了发现很简单,把方法分享一下.1.原理.查看NSLocalizedStrin ...
- HDU -2546饭卡(01背包+贪心)
这道题有个小小的坎,就是低于5块不能选,大于5块,可以任意选,所以就在初始条件判断一下剩余钱数,然后如果大于5的话,这时候就要用到贪心的思想,只要大于等于5,先找最大的那个,然后剩下的再去用背包去选择 ...
- Linux crontab命令
--常用参数:crontab -l //查看当前用户下的cron任务crontab -e //编辑当前用户的定时任务crontab -u jo ...
- web04--cookie
1.创建1.jsp <body> <form action="cookie/2.jsp" method="post"> 姓名:<i ...
- asp.net设置元素css的属性
controls.style.Add("css名称","css值") 添加class规则 control.cssclass="str_cssname& ...
- SQL 去除小数点后无效 0 的方法
select convert(float,10.0000) 就是这么简单
- 启动android程序报错
提示错误如下: The connection to adb is down, and a severe error has occured. [2010-03-11 09:36:56 - HelloO ...
- Linux下批量替换文件内容方法
1:查找find . -type f -name "*.html"|xargs grep ‘yourstring’ 2:查找并替换find -name '要查找的文件名' | xa ...
- 获取subview
通常我们在view层级里面对subView的操作可以通过两种方式:1.保留一个subview的引用,然后在类中通过该引用对该subview进行操作,但是要注意在适当的位置添加内存维护的代码,退出前手动 ...