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. [转]下拉按钮 C#_Winform 自定义控件

    [https://workspaces.codeproject.com/elia-sarti/splitbutton-an-xp-style-dropdown-split-button] using ...

  2. linux 守护进程 daemon

    Linux的Service/Daemon你真的懂了吗? Linux 守护进程的启动方法 linux系统编程之进程(八):守护进程详解及创建,daemon()使用 linux守护进程 daemon 详解

  3. dubbo 官方参考手册~备案(防止哪天阿里一生气把dubbo给删除了)

          首页  ||  下载  ||  用户指南  ||  开发者指南  ||  管理员指南  ||  培训文档  ||  常见问题解答  ||  发布记录  ||  发展路线  ||  社区 E ...

  4. javascript中setInterval制作跑马灯的效果

    html代码: javascript代码 <script type="text/javascript"> function scroll() { var title = ...

  5. 如何分析 WindowsDump:Dump 起源与初始设置

    https://www.qcloud.com/community/article/511817 转者注:让我感觉以前看蓝屏都白看了~~~原来蓝屏也可以分析具体原因. 适用场景:Windows 系列系统 ...

  6. js中, match和exec方法的区别

    1. 来源分别为: string.match(reg) 和 RegExp.exec(str): 2. 区别 >  现有 字符串s1 和 正则对象 r1.     目标: 抽出s1中的所有电话号码 ...

  7. Python - Django - ORM 实例

    准备工作: 首先创建一个名为 Py_Django 的数据库 新建项目,名为 mysite0 创建完成后需要进行几项配置 mysite0/settings.py 下 首先是 html 文件相关 其次是数 ...

  8. mysql更新(八) 可视化工具Navicat的使用 索引

    17-索引   一.索引的介绍 数据库中专门用于帮助用户快速查找数据的一种数据结构.类似于字典中的目录,查找字典内容时可以根据目录查找到数据的存放位置吗,然后直接获取. 二 .索引的作用 约束和加速查 ...

  9. 显式锁(二)Lock接口与显示锁介绍

    一.显式锁简介    显式锁,这个叫法是相对于隐式锁synchronized而言的,加锁和解锁都要用户显式地控制.显示锁Lock是在Java5中添加到jdk的,同synchronized一样,这也是一 ...

  10. linux更换shell外壳zsh

    linux-外壳内核与shell的关系 内核处于外壳之中,通过外壳与shell(命令行)交互 外壳可以更换 ############################################## ...