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. jq的$.each遍历数组

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. mybaties实体的 Mapper.xml文件中自定义sql时模糊查询的写法

    <select  id=selectByNameLike" parameterType="string" resultMap="BaseResultMap ...

  3. MySQL5.7 添加、删除用户与授权

    mysql -uroot -proot 例子: 创建用户mysql> CREATE USER 'xiaoyaoji'@'%' IDENTIFIED BY 'xiaoyaoji';Query OK ...

  4. Naming Company CodeForces - 794C

    Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little thi ...

  5. JavaMail实现邮件的发送

    1,拷贝mail.jar 和activation.jar到项目中 2,开启邮箱的 POP3/SMTP服务,以QQ邮箱为例 进去QQ邮箱-->设置-->账号-->进行设置如下图 注意: ...

  6. 多条select语句的合并

    select (') , (') , (select max(ssq) from DW_MARK_FXJS.F_GS_YCXJJDCSY@new_zgxt ) from dual 这样就不用傻傻的执行 ...

  7. Tomcat报错:Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/JFreeChartTest]]

    最好把项目移除,然后在tomcat的webapps发布路径下也把项目文件删掉,重新部署就好了,原因是可能在tomcat的remove覆盖中以前的文件有所保留导致冲突,亲测有效

  8. 使用idea+Tomcat搭建servlet服务器

    1.使用java 搭建一个简单的Servlet 服务器 https://blog.csdn.net/qq_35164169/article/details/76146655

  9. 【HNOI】 lct tree-dp

    [题目描述]给定2-3颗树,每个边的边权为1,解决以下独立的问题. 现在通过连接若干遍使得图为连通图,并且Σdis(x,y)最大,x,y只算一次. 每个点为黑点或者白点,现在需要删除一些边,使得图中的 ...

  10. Tornado 安装及简单程序示例

    1.安装步骤:tar xvzf tornado-3.2.tar.gz cd tornado-3.2 python setup.py build sudo python setup.py install ...