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. UFLDL课程学习(二)

    章节地址:http://ufldl.stanford.edu/tutorial/supervised/LogisticRegression/ 章节名称:逻辑回归 (Logisitic Regressi ...

  2. spring aop原理分析

    持续更新... aop跟java代理模式有关. java.lang.reflect.Proxy java.lang.reflect.InvocationHandler 工厂模式用到java反射. ao ...

  3. mySQL优化 my.ini 配置说明

    [mysqld] port = 3306 serverid = 1 socket = /tmp/mysql.sock skip-name-resolve #禁止MySQL对外部连接进行DNS解析,使用 ...

  4. 一个打砖块的小游戏1.0 KILL THE BLOCKS !

    /******************************************** * 程序名称:MR.DUAN 的打砖块(KILL THE BLOCKS !) * 作 者:WindAutum ...

  5. js 定义函数的几种方法 以及如何调用

    /*1.方法调用模式: 先定义一个对象,然后在对象的属性中定义方法,通过myobject.property来执行方法,this即指当前的myobject 对象.*/ var car = { carId ...

  6. Ubuntu 13.04 用Sublime Text 2 编译运行 JAVA

    将下面的代码粘贴到JavaC.sublime-build中: { "cmd": ["javac \"$file_name\" && j ...

  7. IE兼容HTML5

    <!--[if lt IE9]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js" ...

  8. JAVA - 多线程的同步

    多线程的同步 1. 锁对象. 应用场景:当某个数据可能被其他线程修改时,给涉及到数据的方法上锁,保证同一时刻只有拥有这个锁的线程能访问该数据,其他要调用这个方法的线程被阻塞.注意:必须是不同线程访问同 ...

  9. 转 Android - 文件操作

    一.资源文件的读取: 1) 从resource的raw中读取文件数据: String res = ""; try{ //得到资源中的Raw数据流 InputStream in = ...

  10. 体验Lua

    想用之和NGINX结合,终结公司混乱的NGINX配置 玩起来先,感觉很精简,很实用哟. print("hello world") a={,} b=a print(a==b,a~=b ...