题目

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;
}
};

GitHub测试程序源码

LeetCode(56)Merge Intervals的更多相关文章

  1. LeetCode(56):合并区间

    Medium! 题目描述: 给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18] ...

  2. LeetCode(88)Merge Sorted Array

    题目 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note ...

  3. LeetCode(40)-Merge Sorted Array

    听到初爱有感 开头啰嗦两句,刚在做算法题目的时候,听到了杨宗纬的<初爱>,突然有了一种本科时候的感觉,想想自己现在研二了,青春喂了狗,我果断喝了一罐啤酒,循环这首歌到吐-.. 题目: Gi ...

  4. LeetCode(23)Merge k Sorted Lists

    题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...

  5. 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 ...

  6. 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 ...

  7. Leetcode(1)两数之和

    Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...

  8. Leetcode(7)整数反转

    Leetcode(6)Z字形变换 [题目表述]: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 第一次:转字符串处理 执行用时:40 ms: 内存消耗:11.6MB 效果: ...

  9. Linux学习笔记(11)linux网络管理与配置之一——配置路由与默认网关,双网卡绑定(5-6)

    Linux学习笔记(11)linux网络管理与配置之一——配置路由与默认网关,双网卡绑定(5-6) 大纲目录 0.常用linux基础网络命令 1.配置主机名 2.配置网卡信息与IP地址 3.配置DNS ...

随机推荐

  1. git提交报错SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version

    git push报错 git push origin master Administrator@FREESKYC-92DB80 /e/git/ouyida3/ouyida3.github.io (ma ...

  2. iOS NSDictionary <--> NSString(JSON) in Objc

    NSDictionary --> NSString + (NSString*)stringINJSONFormatForObject:(id)obj { NSData *jsonData = [ ...

  3. 【数据结构(C语言版)系列一】 线性表

    最近开始看数据结构,该系列笔记简单记录总结下所学的知识,更详细的推荐博主StrayedKing的数据结构系列,笔记部分也摘抄了博主总结的比较好的内容. 一些基本概念和术语 数据是对客观事物的符号表示, ...

  4. [CF997E] Good SubSegment

    Description Transmission Gate 给你一个长度为n的排列P,定义一段子区间是好的,当且仅当这个子区间内的值构成了连续的一段.例如对于排列\(\{1,3,2\}\),\([1, ...

  5. LightOj 1074 Extended Traffic (spfa+负权环)

    题目链接: http://lightoj.com/volume_showproblem.php?problem=1074 题目大意: 有一个大城市有n个十字交叉口,有m条路,城市十分拥挤,因此每一个路 ...

  6. 随机L系统分形树 分类: 计算机图形学 2014-06-01 23:27 376人阅读 评论(0) 收藏

    下面代码需要插入到MFC项目中运行,实现了计算机图形学中的L系统分形树. class Node { public: int x,y; double direction; Node(){} }; CSt ...

  7. 转 Docker Swarm vs Kubernetes

    容器化已经改变我们部署软件和微服务开发的方式.如果你刚听说容器, 这篇博客帮你入门. 什么是容器编排 容器能够把服务打包成基本单元,你可以把它部署到任何地方:本地机器.测试环境或者生产系统.但是在生产 ...

  8. android开发学习--网络请求框架RxJava+Retrofit

    看了好多的博客,终于弄清楚了Rxjava和Retrofit,给大家推荐几个不错的文章: https://gank.io/post/56e80c2c677659311bed9841 这个文章是只用Ret ...

  9. AJPFX理解反射及反射的应用

    怎么理解反射,反射的应用        反射就是把Java类中的各种成分映射成相应的Java类.        一般情况下我们要解决某个问题,先找到相关的类,创建该类的对象,然后通过该对象调用对应的方 ...

  10. HTML标签,简单归纳

    列表标签 有序列表: <ol><li></li></ol> 无序列表: <ul><li></li></ul&g ...