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

解题思路:

1、将区间按照起始位置从小到大排序;

2、一次遍历,如果发现当前区间起始小于上一个区间结束,则进行合并;

解题步骤:

1、因为需要比较结构体,所以编写比较函数<

2、新建一个结果数组,保存合并后的结果;

3、对输入数组进行排序;

4、将第一个区间放入结果数组中;

5、从第二个区间开始遍历原数组:

  (1)如果当前遍历到的区间start < 结果数组最后一个区间的end,则更改结果数组最后一个区间的end;

  (2)否则,将当前遍历到的区间插入结果数组中;

代码:

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

【Leetcode】【Hard】Merge Intervals的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. LeetCode解题报告—— Jump Game & Merge Intervals & Permutation Sequence

    1. Jump Game Given an array of non-negative integers, you are initially positioned at the first inde ...

  6. 【LeetCode每天一题】Merge Intervals(合并区间)

    Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...

  7. 【leetcode刷题笔记】Merge Intervals

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

  8. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  9. 【LeetCode每天一题】 Merge k Sorted Lists(合并K个有序链表)

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...

  10. 【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 ...

随机推荐

  1. empty isset array_key_exists 的区别

    empty: 参数为0或为NULL时(如上面列子),empty均返回TRUE,详细情况可以参见empty官方手册 isset: 参数为NULL时,返回FALSE,0与NULL在PHP中是有区别的,is ...

  2. time时间处理

    time模块的使用 import time print(time.time()) 输出: 1476798696.6639342 #表示从1970 年 1 月 1 日 00:00:00到当前的秒数 pr ...

  3. 如何使用XproerUI库(WTL)-XproerUI界面库教程

    版权所有 2009-2015 荆门泽优软件有限公司 保留所有权利 产品首页:http://www.ncmem.com/apps/xproerui/index.asp 开发文档(SkinStudio): ...

  4. args

    java 中args一般存在main主类方法内,String args[ ]或者String[ ] args表示给主方法传一个字符串数组. 而args是一个字符串数组的变量名,不是关键字,是argum ...

  5. __iter__

    [__iter__] 如果一个类想被用于for ... in循环,类似list或tuple那样,就必须实现一个__iter__()方法,该方法返回一个迭代对象,然后,Python的for循环就会不断调 ...

  6. DOS批量递归删除文件夹

    @echo off for /d /r E:\test01\ %%i in (test) do rd /s /q "%%i" pause

  7. 缓存大全(Memcached、redis、RabbitMQ )

    Memcached: 简介.安装.使用 python操作Memcached Memcached天生支持集群 Redis: 简介.安装.使用.实例 Python操作Redis String.Hash.L ...

  8. Strict Standards: Only variables should be passed by reference

    <?php $tag="1 2 3 4 5 6 7"; $tag_sel = array_shift(explode(' ', $tag)); print_r($tag_se ...

  9. [Notes] Reading Notes on [Adaptive Robot Control – mxautomation J. Braumann 2015]

    Reading sources: 1.Johannes Braumann, Sigrid Brell-Cokcan, Adaptive Robot Control (ARC  ) Note: buil ...

  10. Xshell快捷键

    Ctrl + L     清除工作区的内容