leetcode[55] 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].
这题主要会要知道如何对一个node型或者是struct进行排序。就是如何定义sort的比较函数,sort的比较函数本来默认的是小于函数,现在给定的struct类型没有定义的小于函数,所以要自己定义一个。根据左区间排序。
定义好后,进行排序,排好序后,扫面一次判断下一个的开头和前一个的结尾的大小,如果下一个开头大,那么前面一个就是分离的区间,记录答案。如果下一个的开头不比上一个的结尾大,那么就有重叠了,这时候就判断下一个的结尾是否比前一个的结尾大,大的话就替代它。所以我们要有一个指针指向保存了答案的vector的末尾,以便于和下一个开头判断。
/**
* 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 {
private:
static bool myCmp(Interval a, Interval b)
{
return a.start < b.start;
}
public:
vector<Interval> merge(vector<Interval> &intervals)
{
if(intervals.empty()) return intervals;
sort(intervals.begin(), intervals.end(), myCmp);
vector<Interval> ans;
int cnt = ;
while(cnt < intervals.size() )
{
Interval tmp;
tmp = intervals[cnt];
while(cnt + < intervals.size() && intervals[cnt + ].start <= tmp.end )
{
if (intervals[cnt + ].end >= tmp.end)
{tmp.end = intervals[cnt + ].end;cnt++;}
else cnt++; // 排除下一个区间已经被上一个区间包围的例子
}
cnt++;// 记得要再加一,从下一个开始
ans.push_back(tmp);
}
return ans;
}
};
更详细请参见这位
/**
* 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 {
private:
static bool comp(Interval a, Interval b)
{
return a.start < b.start;
}
public:
vector<Interval> merge(vector<Interval> &intervals) {
if(intervals.empty())return intervals;
sort(intervals.begin(), intervals.end(), comp);
vector<Interval> res;
res.push_back(intervals[]);
for(int i = ; i < intervals.size(); i++)
{
Interval &p = res.back(); // 找到末尾,为了和下一个开头比较
if(intervals[i].start > p.end)res.push_back(intervals[i]);
else if(intervals[i].end > p.end)p.end = intervals[i].end;
}
return res;
}
};
leetcode[55] Merge Intervals的更多相关文章
- [Leetcode Week2]Merge Intervals
Merge Intervals题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/merge-intervals/description/ Descript ...
- 【LeetCode】Merge Intervals 题解 利用Comparator进行排序
题目链接Merge Intervals /** * Definition for an interval. * public class Interval { * int start; * int e ...
- 【leetcode】Merge Intervals
Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ...
- 【leetcode】Merge Intervals(hard)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 【leetcode】 Merge Intervals
Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ...
- 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: 56. Merge Intervals(Medium)
1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域 ...
- Java for LeetCode 056 Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...
随机推荐
- Flex里的特效
Flex中提供了丰富的效果组件.因为效果是一种依据时间渐变的过程,因此全部效果都具有duration属性,用来设置播放时间(以毫秒为单位).也能够通过设置repeatCount属性和repeatD ...
- hdu4570Multi-bit Trie (间隙DP)
Problem Description IP lookup is one of the key functions of routers for packets forwarding and clas ...
- 移动端 延迟加载echo.js的使用
浏览器支持ie8+ <img src="img/blank.gif" alt="" data-echo="img/album-1.jpg&q ...
- 不同版本的SQL Server之间数据导出导入的方法及性能比较
原文:不同版本的SQL Server之间数据导出导入的方法及性能比较 工作中有段时间常常涉及到不同版本的数据库间导出导入数据的问题,索性整理一下,并简单比较下性能,有所遗漏的方法也欢迎讨论.补充. 0 ...
- NSIS:判断程序是否运行并进行卸载
原文NSIS:判断程序是否运行并进行卸载 今天在评论里看到网友说要一个这样的功能,就简单写了一个,本来想做360杀手来着,但遗憾的是我从来不用360的东西,所在电脑上也没有360相关的软件进行测试,所 ...
- Java回合阵列List
package com.mine.practice.arrtolist; import java.util.ArrayList; import java.util.Arrays; import jav ...
- WebGL 支持测试,并已支持的浏览器版本摘要
WebGL 支持情况检測与已支持浏览器版本号汇总 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公 ...
- Pro Aspnet MVC 4读书笔记(3) - Essential Language Features
Listing 4-1. The Initial Content of the Home Controller using System; using System.Collections.Gener ...
- android 拍照注意问题
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, reqCode) ...
- ASP.NET MVC+EF框架+EasyUI实现权限管理系列(15)-用户登录详细错误和权限数据库模型设计
原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(15)-用户登录详细错误和权限数据库模型设计 ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇) ...