Merge Intervals——STL的应用
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].
这里先排序,然后在合并,只要想到了先排序,这道题就好做了,运用STL中sort函数,然后自己写个比较大小的重载函数,虽然之前看到过写比较大小的重载函数,但是没自己写过,这次是自己写,是一大进步,并且之前没用过vector中的back函数,这次也是第一次用,写这个代码学到点东西~~
/**
* 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 cmp(const Interval&a,const Interval&b)
{
return a.start<b.start;
}
vector<Interval> merge(vector<Interval>& intervals) {
if(intervals.size()==||intervals.size()==)
return intervals;
sort(intervals.begin(),intervals.end(),cmp);
vector<Interval> res;
res.push_back(intervals[]);
for(int i=;i<intervals.size();i++)
{
Interval temp=res.back();
if(temp.end>=intervals[i].start&&temp.end<intervals[i].end)
{
temp.end=intervals[i].end;
res.pop_back();
res.push_back(temp);
} else if(temp.end<intervals[i].start)
res.push_back(intervals[i]);
}
return res;
}
};
Merge Intervals——STL的应用的更多相关文章
- 【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 ...
- 60. Insert Interval && Merge Intervals
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [Leetcode][Python]56: Merge Intervals
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...
- [array] leetcode-56. Merge Intervals - Medium
leetcode-56. Merge Intervals - Medium descrition Given a collection of intervals, merge all overlapp ...
- 【leetcode】 Merge Intervals
Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ...
- Insert Interval & Merge Intervals
Insert Intervals Given a non-overlapping interval list which is sorted by start point. Insert a new ...
- 56. Merge Intervals 57. Insert Interval *HARD*
1. Merge Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[ ...
随机推荐
- Redis、Memcache
★ Redis redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sor ...
- Eclipse NDK 打印LOG信息(都在jni目录下操作)
http://blog.csdn.net/u013045971/article/details/46448975 1 在.c文件中,引用头文件,定义TAG.LOG宏: #include <and ...
- Linux之初试驱动20160613
这篇文章主要介绍一下Linux内核下的驱动结构与书写,以及介绍Linux下简单使用驱动的应用程序: 首先我们直接看使用驱动的简单应用程序: #include <sys/types.h> # ...
- opencv学习--透视变化
透视变换和仿射变换具有很大的相同特性,前面提到了放射变化,这里再次把它拿出和透视变换进行比较 #include"cv.h" #include"highgui.h" ...
- mac os x之解决npm安装包失败,或者nodejs工程缺少依赖
在国内做开发,由于各种各样的原因,导致网络总是那么不好,对于我们前端开发者,在使用npm的时候很可能因为网络问题导致包安装失败,然后我们又匆匆启动项目,导致缺少依赖等各种问题,下面将会介绍一个淘宝的n ...
- annot refer to a non-final variable * inside an inner class defined in a different method"错误解析
在使用Java局部内部类或者匿名内部类时,若该类调用了所在方法的局部变量,则该局部变量必须使用final关键字来修饰,否则将会出现编译错误“Cannot refer to a non-final va ...
- 通过反射获取T.class代码片段
说明 持久化框架MyBatis和Hibernate中我们多多少少都会自己取写工具类!但是我们一般都会处理结果集转换成持久化对象,但是我们都要使用类! 代码片段 abstract public clas ...
- 我的CCF备考指南
CCF计算机软件能力认证(简称CCF CSP认证). 认证涉及知识点: 认证内容主要覆盖大学计算机专业所学习的程序设计.数据结构.算法以及相关的数学基础知识.包括但不限于: (1)程序设计基础 逻辑与 ...
- css 常用苹方字体
// 苹方-简 常规体 font-family: PingFangSC-Regular, sans-serif; // 苹方-简 极细体 font-family: PingFangSC-Ultrali ...
- rocketmq单机搭建
RocketMQ 是alibaba开源的消息队列. 本文使用的是开源版本v3.18 系统: centos6.x最小化安装 需要用到的软件包: jdk-7u67-linux-x64.tar.gz ali ...