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. print 出来的信息添加到text文件里

  2. CA单向认证和双向认证的区别?

    1:单向认证,内容会被串改吗?

  3. Hessian与Webservice的区别

    Hessian:hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能,相比WebService,Hessian更简单.快捷. 采用的是二进制RPC协议,因为 ...

  4. XML,XSD,XSLT应用场景

    XML:数据交换的标准  1.数据通信: 其实HTTP就是标准的报文格式,早开发中,设计报文的格式是可以看出这个系统的好坏  2.配置文件:设计一个良好的配置文件比写代码要难,比如Spring的配置文 ...

  5. bash下. : () {} [] [[]] (())的解释

    bash下有很多像{}.[]等一些符号命令,下面是我对一些常用的符号命令的学习笔记,若有错误或纰漏望各位兄弟指正. 一..(source).(点)与source命令一样,从文件中读取并执行命令,无论该 ...

  6. 温故而知新-PHP文件操作函数

    1 文件操作流程 打开文件->读取或者写入文件->关闭文件 fopen->fread,fwrite->fclose fopen可以打开ftp或者http协议的文件,前提示对方支 ...

  7. smokeping配置方法

    smokeping配置加Nginx https://lala.im/2821.html  (不完整)

  8. spring boot 整合案例

    github  :   https://github.com/nbfujx/springBoot-learn-demo

  9. scheduling.quartz.CronTriggerBean has interface org.quartz.CronTrigger as super class

    转自:https://blog.csdn.net/lzj0470/article/details/17786587 quartz版本号:2.1.7 错误: Caused by: <a href= ...

  10. Mysql 主- 开启binlog

    https://www.cnblogs.com/martinzhang/p/3454358.html my.cnf 添加 log_bin=mysql-bin 开启日志,然后重启mysql服务器. 查看 ...