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

这里先排序,然后在合并,只要想到了先排序,这道题就好做了,运用STL中sort函数,然后自己写个比较大小的重载函数,虽然之前看到过写比较大小的重载函数,但是没自己写过,这次是自己写,是一大进步,并且之前没用过vector中的back函数,这次也是第一次用,写这个代码学到点东西~~

/**
* 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 cmp(const Interval&a,const Interval&b)
{
return a.start<b.start;
}
vector<Interval> merge(vector<Interval>& intervals) {
if(intervals.size()==||intervals.size()==)
return intervals;
sort(intervals.begin(),intervals.end(),cmp);
vector<Interval> res;
res.push_back(intervals[]);
for(int i=;i<intervals.size();i++)
{
Interval temp=res.back();
if(temp.end>=intervals[i].start&&temp.end<intervals[i].end)
{
temp.end=intervals[i].end;
res.pop_back();
res.push_back(temp);
} else if(temp.end<intervals[i].start)
res.push_back(intervals[i]);
}
return res;
}
};

Merge Intervals——STL的应用的更多相关文章

  1. 【leetcode】Merge Intervals

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

  2. 【leetcode】Merge Intervals(hard)

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

  3. 60. Insert Interval && Merge Intervals

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  4. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

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

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

  6. [array] leetcode-56. Merge Intervals - Medium

    leetcode-56. Merge Intervals - Medium descrition Given a collection of intervals, merge all overlapp ...

  7. 【leetcode】 Merge Intervals

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

  8. Insert Interval & Merge Intervals

    Insert Intervals Given a non-overlapping interval list which is sorted by start point. Insert a new ...

  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. 【DP】【P2224】】【HNOI2001】产品加工

    传送门 Description 某加工厂有\(A\).\(B\)两台机器,来加工的产品可以由其中任何一台机器完成,或者两台机器共同完成.由于受到机器性能和产品特性的限制,不同的机器加工同一产品所需的时 ...

  2. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)A模拟 B三分 C dfs D map

    A. Andryusha and Socks time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  3. HDU3949 XOR (线性基)

    HDU3949 XOR Problem Description XOR is a kind of bit operator, we define that as follow: for two bin ...

  4. java入门实现转换

    设计思想 首先不用多说就是建立最基础的java创建,然后抛出一个异常处理来替我们检测用户的输入,这一点十分重要.然后就要进行输入工作,不必多说,网上的教程有一个Scanner的输入方法,我们引入一下. ...

  5. PAT (Advanced level) 1003. Emergency (25) Dijkstra

    As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...

  6. [洛谷P2375] [NOI2014]动物园

    洛谷题目链接:[NOI2014]动物园 题目描述 近日,园长发现动物园中好吃懒做的动物越来越多了.例如企鹅,只会卖萌向游客要吃的.为了整治动物园的不良风气,让动物们凭自己的真才实学向游客要吃的,园长决 ...

  7. 【设计模式】 模式PK:命令模式VS策略模式

    1.概述 命令模式和策略模式的类图确实很相似,只是命令模式多了一个接收者(Receiver)角色.它们虽然同为行为类模式,但是两者的区别还是很明显的.策略模式的意图是封装算法,它认为“算法”已经是一个 ...

  8. 文件字节大小显示成M,G和K

    //字节大小,K,M,G public static final long KB = 1024; public static final long MB = KB * 1024; public sta ...

  9. concurrent.futures 使用及解析

    from concurrent.futures import ThreadPoolExecutor, as_completed, wait, FIRST_COMPLETED from concurre ...

  10. BTA 常问的 Java基础40道常见面试题及详细答案(山东数漫江湖))

    八种基本数据类型的大小,以及他们的封装类 引用数据类型 Switch能否用string做参数 equals与==的区别 自动装箱,常量池 Object有哪些公用方法 Java的四种引用,强弱软虚,用到 ...