题目

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. border-radius的参数

    border-radius的参数: 据w3c上的官方解释,是这样子的: border-radius: 1-4 length|% / 1-4 length|%;1-4指的是radius的四个值,leng ...

  2. poj 1258 Agri-Net prim模板 prim与dijkstra的区别

    很裸地求最小生成树的题目.题意就不多说了,最重要的就是记录一下学会了prim算法. 初学prim,给我的第一感觉就是和dijkstra好像啊,感觉两者的区别还是有的: 1:prim是求最小生成树的算法 ...

  3. _bzoj1503 [NOI2004]郁闷的出纳员【Splay】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1503 由于初始工资未达到下限而离开的员工不算在离开人数之内...坑爹... 然后就是写kth ...

  4. 递推+高精度+找规律 UVA 10254 The Priest Mathematician

    题目传送门 /* 题意:汉诺塔问题变形,多了第四个盘子可以放前k个塔,然后n-k个是经典的汉诺塔问题,问最少操作次数 递推+高精度+找规律:f[k]表示前k放在第四个盘子,g[n-k]表示经典三个盘子 ...

  5. Oracle10g初探DBCA

    Database Configuration Assistant. [oracle@dbsrv3 bin]$ pwd /opt/oracle//bin [oracle@dbsrv3 bin]$ ./d ...

  6. Latex排版工具的使用(一) 分类: Latex 2014-06-14 22:52 448人阅读 评论(0) 收藏

    使用Latex可以排版出漂亮的论文,尤其适合对含有数学公式论文的排版. 下面编写第一Latex源文件,实现对两个数学公式的排版: 新建文件first.tex: \documentclass{artic ...

  7. HttpURLConnection教程

    1.Class Overview An URLConnection for HTTP (RFC 2616) used to send and receive data over the web. Da ...

  8. 转-MySQL for Mac 安装和基本操作

    一.安装mysql 1.mysql下载地址:http://dev.mysql.com/downloads/mysql/ 2.安装软件包位于硬盘映象(.dmg)文件中,必须首先双击搜索起中的图标来安装该 ...

  9. HBase简介(很好的梳理资料) 转

    一. 简介 history started by chad walters and jim 2006.11 G release paper on BigTable 2007.2 inital HBas ...

  10. Android开发中使用startActivityForResult()方法从Activity A跳转Activity B出现B退出时A也同时退出的解决办法

    最近一个 App 中用到了 startActivityForResult() 方法,使用的时候却出现了一些问题,比如我在 Activity A 中调用该方法向 Activity B 中跳转,如果 B  ...