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

解题思路: 根据start对区间进行排序,然后依次遍历进行区间合并。

/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
bool cmp(const Interval &a, const Interval &b){
return a.start < b.start;
}
class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
if(intervals.size() == )
return intervals;
sort(intervals.begin(), intervals.end(), cmp);
vector<Interval> result;
Interval mover = intervals[];
for(int i = ; i < intervals.size(); ++i){
if(mover.end < intervals[i].start){
result.push_back(mover);
mover = intervals[i];
}else{
mover.end = max(mover.end, intervals[i].end);
}
}
result.push_back(mover);
return result;
}
};

【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

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

  3. 【leetcode】Merge Intervals(hard)

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

  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. 关于dreamweaver的软件测评

    最近在用javascript编写程序,于是便用到了dreamweaver .所以,想写一个关于dreamweaver的软件测评. 学生本人使用的是dreamweaver 8.首先,谈谈本人使用感受,打 ...

  2. Linux虚拟机下与主机通信

    1.更改虚拟机ip和主机ip同一网段 2.配置虚拟机的网络适配器 3.主机进行ping测试

  3. [读书笔记]Linux命令行与shell编程读书笔记01

    1. Linux的组成部分 1)linux内核(kernel) 2)GNU工具链 3)GUI/CLI工作几面(shell) 4)应用程序(app) 2Linux内核的主要工作: 1) 管理内存 2)管 ...

  4. CAS登陆过程UML中文版

    如果大家图片显示看不请,可以点击图片右键:在新窗口中打开图片,进行查看 名词解释 CASTGC:向cookie中添加该值的目的是当下次访问 认证中心 时,浏览器将Cookie中的TGC携带到服务器,服 ...

  5. Review software requirements specification and create test scenarios (what to test for a certain functionality)

    1 srs2 what to test3 establish guidelines on how this deliverable is to be presented , the template4 ...

  6. 在做销售录入界面时,如何使用dbgrid?(50分)

    给你段源码看一看用stringgird做得: procedure Tfrmingoods.adddata ; var i:integer; begin do begin ,i])=trim(goods ...

  7. html5 视频和音频

    视频:html5支持视屏文件或者视屏流. html5使用video元素来播放视屏,支持的类型有OGG,MEPG 4,webM,但是不同的浏览器支持类型不同. src可以放置视屏文件的路径,可以使用元素 ...

  8. Essential Phone PH1原生系统常见问题以及解答

    *首先声明原生系统使用Google才是最好的体验. **如果下述问题有更好的解决方法,欢迎各种方式私信我. Q:为什么我的wifi显示无法连接到网络?为什么wifi有x号?如何去掉? A:因为原生系统 ...

  9. windows部分常用命令

    dir 查看内容 md 新建目录 copy 复制 del 删文件 cls 清屏 tasklist 查看运行进程 taskkill /pid xxx 杀死进程xxx taskmgr 打开任务管理器 ms ...

  10. BZOJ3123[Sdoi2013]森林——主席树+LCA+启发式合并

    题目描述 输入 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数.第三行包含N个非负 ...