Merge Intervals

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

 
 
先排序,然后循环合并
 
 /**
* 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:
vector<Interval> merge(vector<Interval> &intervals) {
vector<Interval> result;
if(intervals.empty()) return result; sort(intervals.begin(),intervals.end(),cmp); int start=intervals[].start;
int end=intervals[].end; for(int i=;i<intervals.size();i++)
{
if(intervals[i].start<=end)
{
//注意[1,4],[2,3]的情况
end=end<intervals[i].end?intervals[i].end:end;
}
else
{
result.push_back(Interval(start,end));
start=intervals[i].start;
end=intervals[i].end;
}
} result.push_back(Interval(start,end));
return result;
} static int cmp(const Interval &a,const Interval &b)
{
if(a.start<b.start) return ;
else if(a.start>b.start) return ;
else return a.end<b.end;
}
};

【leetcode】Merge Intervals的更多相关文章

  1. 【LeetCode】Merge Intervals 题解 利用Comparator进行排序

    题目链接Merge Intervals /** * Definition for an interval. * public class Interval { * int start; * int e ...

  2. 【leetcode】Merge Intervals(hard)

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

  3. 【leetcode】 Merge Intervals

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

  4. 【Leetcode】【Hard】Merge Intervals

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

  5. 【LeetCode】Merge Sorted Array(合并两个有序数组)

    这道题是LeetCode里的第88道题. 题目描述: 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nu ...

  6. 【leetcode】Merge Sorted Array

    题目描述 Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assu ...

  7. 【leetcode】Merge Two Sorted Lists

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  8. 【leetcode】Merge k Sorted Lists

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  9. 【leetcode】Merge Two Sorted Lists(easy)

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

随机推荐

  1. centos 7.0 安装

    最小化安装的  主要查看硬盘使用时间 需要安装 smartmontools 这个 [root@localhost ~]# yum install -y smartmontools 已加载插件:fast ...

  2. APP抓链接工具(Fiddler版)

    1.下载Fiddler源代码: http://pan.baidu.com/s/1hqEUK0O 2.修改如下源代码: 3.运行截图:

  3. js中,还真不了解 console

    参考链接: https://segmentfault.com/a/1190000000481884

  4. 求1到n的阶乘

    #include<stdio.h> int main() { int data; ; scanf("%d",&data); ){ int j; ;j<=d ...

  5. 使用Robomongo 连接MongoDB 3.x 报 Authorization failed 解决办法(转)

    最近安装了mongodb3.1.4,并启用了权限验证,在dos窗口下操作没有任何问题,为了维护方便就下载了一个客户端工具Robomongo 0.8.5,用户名.密码的等配置好点解测试,结果连接服务没有 ...

  6. SQL having 子句

    1.为什么存在? 在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用. 2.举例子: SELECT Customer,SUM(OrderPrice) FROM Or ...

  7. maven 错误解决办法集

    一.mavenFailed to execute goal org.apache.maven.plugins:maven-surefire-plugin解决方法 1.测试代码没有获得通过,可以尝重命名 ...

  8. linux中Jetty的安装和配置

    Jetty Jetty 是一个开源的servlet容器,它为基于Java的web内容,例如JSP和servlet提供运行环境.Jetty是使用Java语言编写的,它的API以一组JAR包的形式发布.开 ...

  9. [git]添加项目到git

    写在前面 一直在想把代码托管到git上面,一直没有去研究,最近发现自己写的demo,好多都找不到了,实在是没办法了,耐下心研究了下git.这里通过添加了自己做的demo,算是也是学习下git的操作吧. ...

  10. java项目命名规范

    一.命名规范 1. 项目名全部小写 2. 包名全部小写 3. 类名首字母大写,如果类名由多个单词组成,每个单词的首字母都要大写. 如:public class MyFirstClass{} 4. 变量 ...