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 ...
- iOS中产生随机数的方法
利用arc4random_uniform()产生随机数 Objective-C 中有个arc4random()函数用来生成随机数且不需要种子,但是这个函数生成的随机数范围比较大,需要用取模的算法对随机 ...
- Codeforces Round #514 (Div. 2) C. Sequence Transformation(递归)
C. Sequence Transformation 题目链接:https://codeforces.com/contest/1059/problem/C 题意: 现在有1~n共n个数,然后执行下面操 ...
- HDU 4576 DP
Robot Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Total Sub ...
- JVM之字节码执行引擎
方法调用: 方法调用不同于方法执行,方法调用阶段唯一任务就是确定被调用方法的版本(即调用哪一个方法),暂时还不执行方法内部的具体过程.方法调用有,解析调用,分派调用(有静态分派,动态分派). 方法解析 ...
- Android UI开发第二十四篇——Action Bar
Action bar是一个标识应用程序和用户位置的窗口功能,并且给用户提供操作和导航模式.在大多数的情况下,当你需要突出展现用户行为或全局导航的activity中使用action bar,因为acti ...
- HTML或者JSP页面--执行完某事件后刷新页面,重置表单,清空数据
在提交表单或者执行某个事件之后,如果需要重置表单(即清空表单里的数据) 可以执行下面代码来完成 方式一: self.location.href="userController.do?goAd ...
- 《A First Course in Abstract Algebra with Applications》-chaper1-数论-关于素数
由于笔者在别的专栏多次介绍过数论,这里在<抽象代数基础教程>的专栏下,对于chaper1数论这一章节介绍的方式不那么“入门”. 首先来介绍一个代数中常用也是非常重要的证明方法:数学归纳法. ...
- 字符串类dp的题目总结
熟练掌握回文串吧,大致有dp或者模拟类的吧 ①dp+预处理,懂得如何枚举回文串(一) ②dp匹配类型的题目(二) ③dp+预处理 子串类型 (三) ④字符串的组合数(四) 一:划分成回文串 UVA11 ...
- Spring Web 项目Junit测试报错问题
测试对象是Web项目的Service类,参照网上查到的资料,按如下方式执行时报错, //使用junit4进行单元测试 @RunWith(SpringJUnit4ClassRunner.class) / ...