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

vector<Interval> merge(vector<Interval>& intervals) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
sort(intervals.begin(), intervals.end(), cmp);
vector<Interval> res;
vector<Interval>::iterator it = intervals.begin();
Interval* cur = NULL;
for(it; it != intervals.end(); ++it)
{
if(cur == NULL)
{
cur = &(*it);
continue;
}
if((*cur).end < (*it).start)
{
res.push_back(*cur);
cur = &(*it);
}else{
(*cur).end = (*cur).end > (*it).end ? (*cur).end : (*it).end;
}
}
if(cur != NULL)
res.push_back(*cur);
return res;
}

leetcodequestion_56 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],[ ...

随机推荐

  1. Java HttpClient

    public class WebClient { public static final String POST_TYPE_JSON = "json"; public static ...

  2. java使用内部类的好处及其初始化

    java使用内部类的原因 每个内部类都能独立地继承自一个(接口的)实现,所以无论外围类是否已经继承了某个(接口的)实现,对于内部类都没有影响          java内部类初始化 ForeCatal ...

  3. 解释DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci

    解释DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci 在创建数据库的时候,经常用到一句:CREATE DATABASE `tpcms` DEFAUL ...

  4. iOS 网络与多线程--7.Performselector消息处理方法

    创建一个IOSApp类 IOSApp.h文件 #import <Foundation/Foundation.h> @interface IOSApp : NSObject // 1.添加一 ...

  5. PHP Cookies

    PHP Cookies cookie 常用于识别用户. Cookie 是什么? cookie 常用于识别用户.cookie 是一种服务器留在用户计算机上的小文件.每当同一台计算机通过浏览器请求页面时, ...

  6. ajax+FormData+javascript 实现无刷新上传附件

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  7. linux入门。删除不用到内核,为boot分区释放空间

    在终端中输入如下命令: dpkg --get-selections|grep linux-image 这时会列出系统中所有到内核. 你可以使用 uname -a 查看你当前使用到内核. 然后用 sud ...

  8. 常用CSS代码片断

    单行文本截字 .nowrap { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; word-break: normal; ...

  9. cmd中用PING命令时,出现'Ping'不是内部或外部命令

    在cmd中用PING命令时,出现'Ping' 不是内部或外部命令,也不是可运行的程序或批处理文件.先了解一下内容:1.可执行文件.命令文件和批处理文件以.exe或者.com或者.bat为扩展名的文件分 ...

  10. 使用js 在IE和火狐firfox 里动态增加select 的option

    使用js 在IE和火狐firfox 里动态增加select 的option <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transition ...