题目

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

解答

这题首先根据起始点对Interval进行排序,然后从头开始遍历Interval,对遍历到的每一个Interval(用current表示),将其终止点作为界限,检查其后的Interval的起始点是否小于或等于界限,如果小于或等于界限,则比较该Interval的终止点和界限来决定是否对界限进行更新,并继续检查后面的Interval,否则就将current的起始点和界限作为一个新的Interval记录下来,并继续遍历(当然已经检查过并进行合并操作的Interval就不需要遍历了)。

下面是AC的代码:

/**
 * 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(struct Interval a, struct Interval b){
        return a.start < b.start;
    }
    vector<Interval> merge(vector<Interval>& intervals) {
        vector<Interval> ans;
        sort(intervals.begin(), intervals.end(), compare);
        vector<Interval>::iterator i, j;
        for(i = intervals.begin(); i != intervals.end(); i++){
            int temp = i->end;
            for(j = i + 1; j != intervals.end(); j++){
                if(j->start <= temp){
                    temp = max(temp, j->end);
                }
                else{
                    break;
                }
            }
            j--;
            ans.push_back(Interval(i->start, temp));
            i = j;
        }
        return ans;
    }
};

111

LeetCode OJ 56. Merge Intervals的更多相关文章

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

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

  2. 【LeetCode】56. Merge Intervals

    Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given  ...

  3. 【一天一道LeetCode】#56. Merge Intervals

    一天一道LeetCode系列 (一)题目 Given a collection of intervals, merge all overlapping intervals. For example, ...

  4. 【LeetCode】56. Merge Intervals 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. LeetCode 题解 56. Merge Intervals

    题目大意:给出一组区间,合并他们. 首先是排序,首先看start,start小的在前面.start相同的话,end小的在前面. 排序以后,要合并了. 我自己的笨方法,说实在的问题真的很多.提交了好几次 ...

  6. [leetcode sort]56. Merge Intervals

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

  7. LeetCode OJ:Merge Intervals(合并区间)

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

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

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

  9. 56. Merge Intervals - LeetCode

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

随机推荐

  1. zabbix监控ESXI主机(可用)

    ESXI6.0默认SSH关闭的,打开SSH的方法如下图: SSH打开后,主机会有警报,关闭警报的方法如下图 esxcli system  snmp  set  --communities  publi ...

  2. 阿里云:游戏行业DDoS攻击解决方案

    转自:http://www.gamelook.com.cn/2018/01/319420 根据全球游戏和全球移动互联网行业第三方分析机构Newzoo的数据显示:2017年上半年,中国以275亿美元的游 ...

  3. PostgreSQL 一主多从(多副本,强同步)简明手册 - 配置、压测、监控、切换、防脑裂、修复、0丢失 - 珍藏级

    参考来源: https://github.com/digoal/blog/blob/master/201803/20180326_01.md#postgresql-一主多从多副本强同步简明手册---配 ...

  4. kappa系数

    kappa计算结果为-1~1,通常kappa是落在 0~1 间,可分为五组来表示不同级别的一致性: 0.0~0.20 极低的一致性(slight) 0.21~0.40 一般的一致性(fair) 0.4 ...

  5. 第8章 传输层(3)_TCP协议

    3. 传输控制协议(TCP) 3.1 TCP协议的主要特点 (1)TCP是面向连接的传输层协议.即使用TCP协议之前必须先建立TCP连接.在传送数据完毕之后,必须释放己经建立的TCP连接. (2)每一 ...

  6. linux5.6以下版本的不兼容问题

    之前一直用的都是mysql5.6版本,最近突然使用到了mysql5.1版本,于是在导入数据的时候便出现了很多由于版本不兼容的问题. 1.mysql5.1没有datetime类型,所以对于时间类型,只能 ...

  7. useful tools and website

    1.https://www.processon.com/   在线流程图制作网站 2.http://www.easyicon.net/  专门下载图标的网站 3.https://www.lfd.uci ...

  8. 「2017 山东一轮集训 Day4」棋盘(费用流)

    棋盘模型 + 动态加边 #include<cstdio> #include<algorithm> #include<iostream> #include<cs ...

  9. Java之ConcurrentHashMap

    由于工作中使用到了ConcurrentHashMap,然后查了一波资料,最后整理如下: 1. 描述: ConcurrentHashMap是在Java1.5作为HashTable的替代选择新引入的,是c ...

  10. js 数组的crud操作

    增加push(); 向数组尾添加元素unshift(); 向数组头添加元素向数组指定下标添加元素:可以用Array提供的splice(); var arr = ['a','b','c']; arr.s ...