[LeetCode] 56. Merge Intervals(vector sort)

/**
* 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(Interval &a,Interval &b)
{
return a.start < b.start;
}
vector<Interval> merge(vector<Interval>& intervals)
{
if(intervals.empty()) return vector<Interval> {};
sort(intervals.begin(),intervals.end(),cmp);
vector<Interval> res;
res.push_back(intervals[]);
for(int i = ; i < intervals.size(); i ++)
{
if(intervals[i].start > res.back().end)
res.push_back(intervals[i]);
else
res.back().end = max(res.back().end, intervals[i].end);
}
return res;
}
};
[LeetCode] 56. Merge Intervals(vector sort)的更多相关文章
- 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对象集合,然后对重叠的区域 ...
- leetcode 56. Merge Intervals 、57. Insert Interval
56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...
- 【leetcode】Merge Intervals(hard)
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 ...
- 34.Merge Intervals(合并序列)
Level: Medium 题目描述: Given a collection of intervals, merge all overlapping intervals. Example 1: I ...
- LeetCode OJ:Merge Intervals(合并区间)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8, ...
- Leetcode#56 Merge Intervals
原题地址 排序+合并,没啥好说的 第一次尝试C++的lambda表达式,有种写js的感觉,很神奇 c11就支持了lambda表达式,仔细想想,我学C++大概就是在09~10年,c11还没有发布,不得不 ...
- [LeetCode] 56. Merge Intervals 解题思路
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
随机推荐
- BZOJ2844: albus就是要第一个出场(线性基)
Time Limit: 6 Sec Memory Limit: 128 MBSubmit: 2054 Solved: 850[Submit][Status][Discuss] Descriptio ...
- Linux系统运维基础测试题
1 Linux运维基础测试题(第一关) 通过这段时间学习Linux基础命令,为了检测自己对Linux基础命令掌握的情况,从网上整理13到测试题,并将其整理出来供大家参考学习. 1.1 习题 ...
- js中String 转化为 Date
<script> var s=["2008-8-1","2009/9/2","10/3/2010"]; for(var i=0; ...
- 【实现高可效的代理模式-Squid】
普通正向代理 首先安装squid代理软件包: 端口控制 在squid server端作端口访问控制,把默认的3128端口改为1000端口 同时把squid服务代理端口添加到selinux安全子系统的允 ...
- I/O流、ZIP文档
1) ZIP文档通常以压缩格式存储一个或多个文档.在Java中可以用ZipInputStream读入ZIP文档(即解压文件流),用ZipOutputStream写入ZIP文档(即压缩文件流),无论解压 ...
- Java写Excel(不生成实体文件,写为流的形式)
java 写 Excel(不生成实体文件,写为流的形式) public String exportReportExcel(String mediaCode, List<SimpleMediaRe ...
- MySQL 清除表空间碎片
碎片产生的原因 (1)表的存储会出现碎片化,每当删除了一行内容,该段空间就会变为空白.被留空,而在一段时间内的大量删除操作,会使这种留空的空间变得比存储列表内容所使用的空间更大; (2)当执行插入操作 ...
- php 操作RabbitMQ
本文摘抄自:https://www.cnblogs.com/alin-qu/p/8312874.html php 操作RabbitMQ 基本流程图 如果exchange 没有绑定queue,则消息 ...
- CentOS 同步时间的方法
与时间服务器上的时间同步的方法 1. 安装ntpdate工具 # yum -y install ntp ntpdate 2. 设置系统时间与网络时间同步 # ntpdate cn.pool.ntp ...
- hadoop生态搭建(3节点)-03.zookeeper配置
# https://www.oracle.com/technetwork/java/javase/downloads/java-archive-javase8-2177648.html # ===== ...