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. 杂项:IIS

    ylbtech-杂项:IIS IIS是Internet Information Services的缩写,意为互联网信息服务,是由微软公司提供的基于运行Microsoft Windows的互联网基本服务 ...

  2. 去除adb传输中的^M

    学习sed过程中,在文本中每行追加内容,发现使用adb会在行末追加一个看不到^M. 场景一:adb保存到文件 adb shell ps|head -n 10 > text.txt,使用sed进行 ...

  3. js读取解析JSON类型数据

    原文地址:http://www.ablanxue.com/prone_3691_1.html JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立 ...

  4. html基础代码示例

    文档结构 <!-- 声明文档的类型 标记该文档为HTML5的文件 --> <!DOCTYPE html> <!-- 页面的根节点 --> <!-- html中 ...

  5. 经典算法 BFPRT算法详解

    内容: 1.原始问题     =>  O(N*logN) 2.BFPRT算法    => O(N) 1.原始问题 问题描述:给你一个整型数组,返回其中第K小的数 普通解法: 这道题可以利用 ...

  6. selenium+python自动化86-Chrome正在受到自动软件的控制

    出现问题 1.用selenium启动浏览器出现'Chrome正在受到自动软件的控制' 2.如果不想看到这种讨厌的提示语,启动浏览器时候加个配置就行了 disable-infobars 1.在浏览器配置 ...

  7. 《GPU高性能编程CUDA实战》附录二 散列表

    ▶ 使用CPU和GPU分别实现散列表 ● CPU方法 #include <stdio.h> #include <time.h> #include "cuda_runt ...

  8. HTML5 Canvas ( 图形的像素操作 ) getImageData, putImageData, ImgData.data

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. aspxGridview 根据单元格值得不同,设置单元格字体的颜色(设置和读取值)

    protected void ASPxGridView1_HtmlRowCreated(object sender,DevExpress.Web.ASPxGridView.ASPxGridViewTa ...

  10. java ee7 软件安装和环境配置

    1. java ee sdk 最新版下载地址 Java EE软件开发包(Software Development Kit, SDK) http://www.oracle.com/technetwork ...