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

注意点:

1。并不是像例子中给出的那样,前一个interval必定在后一个interval的前边。

所以我们先要对start进行排序。必须把start小的放在前面,然后按序递增,否则会出现这样的错误

Input:[[2,3],[4,5],[6,7],[8,9],[1,10]]
Output:[[2,3],[4,5],[6,7],[1,10]]
Expected:[[1,10]]
 
2。sort start需要自定义compare函数,注意升序的时候不能定义<=,否则会造成Time Limit Exceeded
原因是sort的compare函数必须满足strict weak ordering。时间复杂度O(nlogn), reference: http://www.cplusplus.com/reference/list/list/sort/
 
3。两个字串长度和 =各子串长度相加的时候,如下例,并没有overlap。仅当[1,4],[4,6]才有overlap。 
[[1,4],[5,6]]
Output:[[1,6]]
Expected:[[1,4],[5,6]]
/**
* 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 v1, Interval v2)
{
if(v1.start < v2.start)
return true;
else if(v1.start > v2.start)
return false;
else
return v1.end < v2.end;
} vector<Interval> merge(vector<Interval>& intervals) {
vector<Interval> result;
if(intervals.empty()) return result; sort(intervals.begin(),intervals.end(),compare);
result.push_back(intervals[]);
for(int i = ; i < intervals.size(); i++){
if(result[result.size()-].end >= intervals[i].end) //totally contain
continue; if(result[result.size()-].end >= intervals[i].start){//there's overlap
result[result.size()-].end = intervals[i].end;
}
else{ //there's no overlap
result.push_back(intervals[i]);
}
} return result;
}
};

56. Merge Intervals (Array; Sort)的更多相关文章

  1. leetcode 56. Merge Intervals 、57. Insert Interval

    56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...

  2. 刷题56. Merge Intervals

    一.题目说明 题目是56. Merge Intervals,给定一列区间的集合,归并重叠区域. 二.我的做法 这个题目不难,先对intervals排序,然后取下一个集合,如果cur[0]>res ...

  3. 56. Merge Intervals - LeetCode

    Question 56. Merge Intervals Solution 题目大意: 一个坐标轴,给你n个范围,把重叠的范围合并,返回合并后的坐标对 思路: 先排序,再遍历判断下一个开始是否在上一个 ...

  4. [Leetcode][Python]56: Merge Intervals

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...

  5. [LeetCode] Merge Intervals 排序sort

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  6. 56. Merge Intervals (JAVA)

    Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...

  7. 56. Merge Intervals

    题目: Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6], ...

  8. [leetcode]56. Merge Intervals归并区间

    Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...

  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. Spring IOC - 控制反转(依赖注入) - 配置初始化和销毁的方法

    在Spring中如果某个bean在初始化之后,或销毁之前要做一些额外操作可以为该bean配置初始化和销毁的我方法,在这些方法中完成需要的功能. 实验: 通过断点调试模式,测试初始化方法和销毁方法的执行 ...

  2. Spring 3.1新特性之一:spring注解之@profile

    前言 由于在项目中使用Maven打包部署的时候,经常由于配置参数过多(比如Nginx服务器的信息.ZooKeeper的信息.数据库连接.Redis服务器地址等),导致实际现网的配置参数与测试服务器参数 ...

  3. request-2高级用法

    会话对象 会话对象让你能够跨请求保持某些参数.它也会在同一个session示例发出的所有请求之间保持cookie cookie与session的区别 1.cookie数据存放在客户的浏览器上,sess ...

  4. Web安全测试指南--会话管理

    会话复杂度: 5.3.2.会话预测: 5.3.3.会话定置: 5.3.4.CSRF: 5.3.5.会话注销: 5.3.6.会话超时:

  5. php file_exists无效解决办法

    一:is_file 和 file_exists 的区别:当文件存在时:is_file 比 file_exists快了N倍当文件不存在时:is_file 比 file_exists慢总之一句话:file ...

  6. Unreal Engine 4 性能优化工具(Profiler Tool)

    转自:http://aigo.iteye.com/blog/2296548 Profiler Tool Reference https://docs.unrealengine.com/latest/I ...

  7. javascript的防篡改对象之preventExtensions()方法

    js在默认情况下,所有的对象都是可扩展的.这也是让很多开发人员头特疼的问题.因为在同一环境中,一不小心就会发生修改了不必要的对象,而自己却不知道. 在ECMAScript5可以解决这种问题了. pre ...

  8. SQL查询日期:

    SQL查询日期: 今天的所有数据:select * from 表名 where DateDiff(dd,datetime类型字段,getdate())=0 昨天的所有数据:select * from ...

  9. Noip往年题目整理

    Noip往年题目整理 张炳琪 一.历年题目 按时间倒序排序 年份 T1知识点 T2知识点 T3知识点 得分 总体 2016day1 模拟 Lca,树上差分 期望dp 144 挺难的一套题目,偏思维难度 ...

  10. 异常处理的设计与重构 pdf

    百度网盘: https://pan.baidu.com/s/1hsQIEGk