题目

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. javascript-添加 class 类 和 移出 class 类 方法

    /* 添加 class 类 和 移出 class 类 方法*/ function addClass(element, className) { if(!new RegExp("(^|\\s) ...

  2. Docker 系列01: Centos7.3 上安装docker

    Docker从1.13版本之后采用时间线的方式作为版本号,分为社区版CE和企业版EE. 社区版是免费提供给个人开发者和小型团体使用的,企业版会提供额外的收费服务,比如经过官方测试认证过的基础设施.容器 ...

  3. KVM总结-KVM性能优化之网络性能优化

    前面已经介绍了KVM CPU优化(http://blog.csdn.net/dylloveyou/article/details/71169463).内存优化(http://blog.csdn.net ...

  4. async 常用函数总结

    待更新. waterfall auto(神器) parallel mapSeries(数据库多条记录操作神器)

  5. linux下mysql-5.6忘记root密码,重置root密码详细过程

      在linux平台下使用mysql过程中忘记了root密码,对于运维和DBA来讲都是一件头疼的事情,下面来讲解下怎么进行重置mysql数据库root 密码: 1.首先停止mysql服务进程: 1 s ...

  6. android 开发 xml绘制shape与Selector与layer-list 一 基础篇

    首先我们先来了解状态效果 android:state_pressed=["true" | "false"]  按下状态 android:state_focuse ...

  7. CentOS7离线安装TIDB

    首先准备一台能够联网,并且操作系统版本与正式版本完全一致的服务器. 安装思路是,通过在线方式获得所有离线安装包,然后导入到正式安装环境中去. yum install -y --downloadonly ...

  8. js-杂记

    js可计算传值 <p>点击按钮计算 x 的值.</p> <button onclick="myFunction()">点击这里</butt ...

  9. tornado运行提示OSError: [WinError 10048] 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。

    找到占用端口的进程,kill掉 netstat -ano | find $(port) kill: tskill $(PID)

  10. 《算法》第一章部分程序 part 1

    ▶ 书中第一章部分程序,加上自己补充的代码,包括若干种二分搜索,寻找图上连通分量数的两种算法 ● 代码,二分搜索 package package01; import java.util.Arrays; ...