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. 使用SQL Server CONVERT() 函数

    语法 CONVERT(data_type(length),data_to_be_converted,style) data_type(length) 规定目标数据类型(带有可选的长度).data_to ...

  2. Error prompt:“wget: unable to resolve host address”---Solution

    //Situation System prompts that:"wget: unable to resolve host address".   //Analysis   Una ...

  3. win8发布 wcf问题

    WCF services don’t run on IIS 8 with the default configuration, because the webserver doesn’t know, ...

  4. exp导出出现:ORA-00904: "POLTYP": invalid identifier

    相关文章:    <exp导出出现:ORA-00904: : invalid identifier>:http://blog.itpub.net/23135684/viewspace-13 ...

  5. 你好,C++(27)在一个函数内部调用它自己本身 5.1.5 函数的递归调用

    5.1.5 函数的递归调用 在函数调用中,通常我们都是在一个函数中调用另外一个函数,以此来完成其中的某部分功能.例如,我们在main()主函数中调用PowerSum()函数来计算两个数的平方和,而在P ...

  6. VS2013 编译 MySql Connector C 6.1.6

    1.下载cmake http://cmake.org/ 2.下载最新版MySql Connector C http://www.mysql.com 3.命令行下,转到源代码目录下,"cmak ...

  7. python查看模块及相关函数帮助

    Question: 如何查看正则表达式模块re及其相关函数的意义 1.终端命令行下 python >> import re >> help(re) Help on module ...

  8. Mac下安装MySQL-python

    前提系统中已安装python.mysql数据库: 第一步(终端输入): $ export PATH=$PATH:/usr/local/mysql/bin 第二步(终端输入): $ sudo pip i ...

  9. Application.Count.ToString()和Application["count"].ToString()的区别

    当属性名中包括特殊字符如 “.”或“-”就不能使用“.”操作符了.操作符只能使用[ ]操作符 为了统计网站的在线人数,我们可以在Global.asa文件中包含如下代码: <SCRIPT LANG ...

  10. RazorEngine 学习笔记

    refer : https://github.com/Antaris/RazorEngine 微软的模板编辑器. Install-Package RazorEngine using RazorEngi ...