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].
分析
给定几个区间,要求合并重叠区间,返回结果;
AC代码
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
//自定义Interval类型元素的升序比较函数
bool cmp(Interval a, Interval b)
{
return a.start < b.start;
}
class Solution {
public:
vector<Interval> merge(vector<Interval>& intervals) {
//如果输入参数为空,则返回空vector
if (intervals.empty())
return vector<Interval>();
int len = intervals.size();
//首先,按照每个Integerval的区间首值进行排序,自定义比较
sort(intervals.begin(), intervals.end() , cmp);
//声明结果
vector<Interval> ret;
vector<Interval>::iterator iter = intervals.begin();
//定义临时变量
Interval temp = intervals[0];
for (int i = 0; i < len; i++)
{
//换一种判断方法
if (intervals[i].start > temp.end)
{
ret.push_back(temp);
temp = intervals[i];
}
else{
temp.end = temp.end > intervals[i].end ? temp.end : intervals[i].end;
}//else
}//for
ret.push_back(temp);
return ret;
}
};
LeetCode(56)Merge Intervals的更多相关文章
- LeetCode(56):合并区间
Medium! 题目描述: 给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18] ...
- LeetCode(88)Merge Sorted Array
题目 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note ...
- LeetCode(40)-Merge Sorted Array
听到初爱有感 开头啰嗦两句,刚在做算法题目的时候,听到了杨宗纬的<初爱>,突然有了一种本科时候的感觉,想想自己现在研二了,青春喂了狗,我果断喝了一罐啤酒,循环这首歌到吐-.. 题目: Gi ...
- LeetCode(23)Merge k Sorted Lists
题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- LeetCode(21)Merge Two Sorted Lists
题目 Merge two sorted linked lists and return it as a new list. The new list should be made by splicin ...
- LeetCode(4)Median of Two Sorted Arrays
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- Leetcode(1)两数之和
Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...
- Leetcode(7)整数反转
Leetcode(6)Z字形变换 [题目表述]: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 第一次:转字符串处理 执行用时:40 ms: 内存消耗:11.6MB 效果: ...
- Linux学习笔记(11)linux网络管理与配置之一——配置路由与默认网关,双网卡绑定(5-6)
Linux学习笔记(11)linux网络管理与配置之一——配置路由与默认网关,双网卡绑定(5-6) 大纲目录 0.常用linux基础网络命令 1.配置主机名 2.配置网卡信息与IP地址 3.配置DNS ...
随机推荐
- 爬虫—Selenium爬取JD商品信息
一,抓取分析 本次目标是爬取京东商品信息,包括商品的图片,名称,价格,评价人数,店铺名称.抓取入口就是京东的搜索页面,这个链接可以通过直接构造参数访问https://search.jd.com/Sea ...
- POJ2482 Stars in Your Window(扫描线+区间最大+区间更新)
Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw you? I stil ...
- logstsh | logstash-input-jdbc 启动错误收集
1: Failed to execute action {:action=>LogStash::PipelineAction::Create/pipeline_id:main, :excepti ...
- Linux 虚拟机配置网络
- vijos1846 [NOIP2013] 华容道【最短路】
传送门:https://vijos.org/p/1983 (其实noip的题各个oj都会有的,就不贴其它传送门了) 这道题真的是,怎么说,我都不知道怎么评价了= =.果然数据量小的题怎么暴力都可以过. ...
- C. Two strings 二分 + 预处理
http://codeforces.com/contest/762/problem/C 第一个串str[],第二个sub[] 预处理出prefix[i]表示sub的前i位和str[]的最长lcs去到s ...
- vim快捷键参考
一. 移动: h,j,k,l: 左,下,上,右. w: 下一个词的词首.W:下一个单词(不含标点). e:下一个词的词尾.E:不含标点. b:上一个词的词首.B:不含标点. <>: v 模 ...
- Java 208道面试题及部分答案
---恢复内容开始--- 一.Java 基础 1.JDK 和 JRE 有什么区别? 答:JRE是java运行时环境,包含了java虚拟机,java基础类库.是使用java语言编写的程序运行所需要的软件 ...
- 大步小步法(BSGS) 学习笔记
\(\\\) BSGS 用于求解关于 \(x\) 的方程: \[ a^x\equiv b\pmod p\ ,\ (p,a)=1 \] 一般求解的是模意义下的指数,也就是最小非负整数解. \(\\\) ...
- Django model 反向引用中的related_name
转自:https://blog.csdn.net/lanyang123456/article/details/68962515 问题: 定义表Apple: class Apple( models.Mo ...