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. Java开发中经典的小实例-(能被3整除,并且十个数换一行)

    import java.util.Scanner;public class Test15 {    public static void main(String[] args) {        // ...

  2. ios下移除原生样式

    之前遇到过这个问题,今天无意中在网上看见博文 记录一下. 设置这个就好: -webkit-appearance : none ;

  3. linux SVNUP显示无法连接主机

    今天开发环境中突然无法连接主机了,我就想到 1.更改svn 链接地址,发现不可取,工程中的每个目录下面都有个.svn文件,修改起来麻烦: 2.建立新文件夹,重新checkout,发现还是无法链接 最后 ...

  4. 学习mongo系列(七)aggregate() ,$group() 管道

    aggregate()聚合,主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果.有点类似sql语句中的 count(*) 接上边的数据库: > db.user.aggregate ...

  5. Linux 系统使用之 VMware Tools安装

    Red Hat Enterprise Linux 4系统中安装VMware Tools 1. 必须以ROOT身份进入Linux 2. 进入linux系统,然后按下 CTRL+ALT组合键,进入主操作系 ...

  6. Krajee 文件上传

    http://plugins.krajee.com/file-input/demo#ajax-uploads 插件官网 项目要个好看点的上传控件,于是搜到了这个. git的地址是 https://gi ...

  7. JDK的安装与配置

    1.什么是JDK(Java Development Kit) 开发工具,javac及基础核心类 运行环境,java及基础核心类 2.目前Java平台的版本 JavaSE(Java Platform S ...

  8. mybatis-config.xml简单笔记

    mybatis-config.xml简单笔记 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...

  9. [原创] RT7 Lite win7旗舰版精简方案

    [原创] RT7 Lite win7旗舰版精简方案 墨雪SEED 发表于 2016-1-26 21:23:54  https://www.itsk.com/thread-362912-1-5.html ...

  10. jquery插件开发基础入门

    jquery插件开发基础入门 入门 编写一个jquery插件开始于给jquery.fn加入新的功能属性,此处添加的对象属性的名称就是你的插件名称 jQuery.fn,myPlugin = functi ...