leetcodequestion_56 Merge Intervals
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的更多相关文章
- 【leetcode】Merge Intervals
Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ...
- 【leetcode】Merge Intervals(hard)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 60. Insert Interval && Merge Intervals
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [Leetcode][Python]56: Merge Intervals
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...
- [array] leetcode-56. Merge Intervals - Medium
leetcode-56. Merge Intervals - Medium descrition Given a collection of intervals, merge all overlapp ...
- 【leetcode】 Merge Intervals
Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ...
- Insert Interval & Merge Intervals
Insert Intervals Given a non-overlapping interval list which is sorted by start point. Insert a new ...
- 56. Merge Intervals 57. Insert Interval *HARD*
1. Merge Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[ ...
随机推荐
- 使用SQL Server CONVERT() 函数
语法 CONVERT(data_type(length),data_to_be_converted,style) data_type(length) 规定目标数据类型(带有可选的长度).data_to ...
- AJAX校验用户名是否存在,焦点离开用户名、点击 【 检 查用户名 】的校验。分别用 XMLHttp 和 JQueryAJAX实现。
XMLHttp方法: $("#name").blur(function () { var xmlhttp = new ActiveXObject("Microsoft. ...
- explicit 关键字
C++ explicit关键字用来修饰类的构造函数,表明该构造函数是显式的,既然有"显式"那么必然就有"隐式",那么什么是显示而什么又是隐式的呢? 如果c++类 ...
- JQUERY1.9学习笔记 之基本过滤器(七) 语言选择器
语言选择器 jQuery( ":lang(language)" ) 描述:选择所有用特定语言指定的标签. 根据标签指定语言的不同给标签上色. <!DOCTYPE html&g ...
- phpcms v9中模板标签使用及联动菜单
{template "content","header"} 调用根目录下phpcms\template\content\header文件 {charset} 字 ...
- java J2EE学习入门
首先学习JAVA基础编程,大学教材就是最简单的了!象写写Helloworld啊 输出水仙花数啊 玩些简单的,慢慢在研究研究流啊,都可以了.然后学习简单的JSP,这个时候多上网上DOWN一些原码.多看看 ...
- 【创建型】Builder模式
生成器模式的主要思想:将产品对象的创建与表现分离开,并且同样的创建过程可以有不同的产品表现. 直白一点可以理解为:待创建的对象是复杂的,一般情况下是需要经过多个步骤的创建后,最终才能将完整产品创建好, ...
- office2003万能密钥
保证有效 OFFICE 2003 : GWH28-DGCMP-P6RC4-6J4MT-3HFDY
- runtime 运行时机制 完全解读
runtime 运行时机制 完全解读 目录[-] import import 我们前面已经讲过一篇runtime 原理,现在这篇文章主要介绍的是runtime是什么以及怎么用!希望对读者有所帮助! ...
- Go Cookie 练习
package main import ( "io" "log" "net/http" ) func main() { http.Handl ...