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

区间合并,分为三种情况

/**
* 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 compare(Interval& a,Interval& b){
return a.start < b.start;
}
vector<Interval> merge(vector<Interval>& intervals){
vector<Interval> res;
int size = intervals.size();
if(size < ) return intervals;
sort(intervals.begin(),intervals.end(),compare);
Interval tmpInterval = intervals[];
for(int i=;i<size;i++){
if(tmpInterval.end < intervals[i].start){
res.push_back(tmpInterval);
tmpInterval = intervals[i];
}else if(tmpInterval.start > intervals[i].end){
res.push_back(intervals[i]);
}else{
tmpInterval.start = min(tmpInterval.start,intervals[i].start);
tmpInterval.end = max(tmpInterval.end,intervals[i].end);
}
}
res.push_back(tmpInterval);
return res;
}
};

Merge Intervals的更多相关文章

  1. 【leetcode】Merge Intervals

    Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given  ...

  2. 【leetcode】Merge Intervals(hard)

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  3. 60. Insert Interval && Merge Intervals

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  4. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  5. [Leetcode][Python]56: Merge Intervals

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...

  6. [array] leetcode-56. Merge Intervals - Medium

    leetcode-56. Merge Intervals - Medium descrition Given a collection of intervals, merge all overlapp ...

  7. 【leetcode】 Merge Intervals

    Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given  ...

  8. Insert Interval & Merge Intervals

    Insert Intervals Given a non-overlapping interval list which is sorted by start point. Insert a new ...

  9. 56. Merge Intervals 57. Insert Interval *HARD*

    1. Merge Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[ ...

  10. Merge Intervals - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Merge Intervals - LeetCode 注意点 区间是无序的 每个区间start一定小于end 解法 解法一:首先以start的值从小到大来 ...

随机推荐

  1. 基于chrome内核的UXSS

    url with a leading NULL byte can bypass cross origin protection. https://code.google.com/p/chromium/ ...

  2. 配置rt-thread开发环境(配置系统,生成系统镜像)

    配置rt-thread开发环境 ===========Python============= 1.Python的下载地址:http://www.python.org/ftp/python/ 链接中有各 ...

  3. 数据转换为json格式的方法

    数据转换为json格式: 如果一张表中存在主外键关系,模板自动生成的类是不可以转换成JSON格式的,此时需要重新写一个类,类前面需加[DataContract],字段前需加[DataMember],实 ...

  4. XAF应用开发教程(八) 汉化与多国语言支持

    使用了XAF开发时,汉化是一个比较常的问题. 要实现汉化很简单: 1.在这里下载汉化资源文件.这里演示的版本是15.1.X的 2.文件下载后将:文件解压到目录    <你的项目>\BIN\ ...

  5. Mac下安装Wireshark,双击闪退

     Mac OS X上使用Wireshark抓包(http://blog.csdn.net/phunxm/article/details/38590561) Mac下安装Wireshark /Appli ...

  6. 【前端开发系列】—— 别说你不会Ajax

    之前一直都是用封装好的Ajax,所以一直很好奇它是如何使用和实现的.这里正好就进行一下学习,下面是Ajax的一个时间图. 设置触发条件 这里模拟一个使用场景,就是在用户登陆时,异步的对用户名以及密码进 ...

  7. 第九章 企业项目开发--分布式缓存Redis(1)

    注意:本章代码将会建立在上一章的代码基础上,上一章链接<第八章 企业项目开发--分布式缓存memcached> 1.为什么用Redis 1.1.为什么用分布式缓存(或者说本地缓存存在的问题 ...

  8. row_number() OVER(PARTITION BY)函数介绍

      OVER(PARTITION BY)函数介绍 开窗函数               Oracle从8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是:对于每个 ...

  9. 循环报数 Java实现

    输入1个数字和多个字符,中间均以空格隔开.假设数字取值为m(范围1~9),后面字符个数为n.假设n个字符围成一圈,从第一个字母开始循环报数,当数到m以后,第m个字母就出列,直到这n个字母全部出列.最后 ...

  10. 32个触发事件XSS语句的总结

    1.onmouseenter:当鼠标进入选区执行代码 <div style="background-color:red" onmouseenter="alert(b ...