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. Java基础以及与C++的一些对比

    这两天回忆一些Java基础,感觉自己很多地方都不是很牢固,也花费在不少时间和不少流量在手机上查资料. 还是写下来这些东西以免再忘记. 同时还是要记住多动手,编程最重要的就是动手敲啊,有想法有疑问就要自 ...

  2. linux 驱动学习笔记01--Linux 内核的编译

    由于用的学习材料是<linux设备驱动开发详解(第二版)>,所以linux驱动学习笔记大部分文字描述来自于这本书,学习笔记系列用于自己学习理解的一种查阅和复习方式. #make confi ...

  3. Eclipse相关设置

    eclipse 默认设置的换行长度, 格式化代码后,同一个方法里面参数也经常被,换行,非常难看. 1.Java代码打开Eclipse的Window菜单,然后 Preferences->Java- ...

  4. VC++ 限制窗口的大小范围的方法

    响应WM_GETMAXMININFO  的消息 处理之 void CMainFrame::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI) { // TODO: Add y ...

  5. java基础之:堆排序

    最近做题目饱受打击,愈发觉得打好基础的重要性,于是乎,决心把基本的排序算法还有数组操作一一实现,目的在于一方面能够得到对JAVA基础的巩固,另一面在实现的过程中发现不足. 今天所实现的堆排序(最大堆) ...

  6. 循环不变量loop invariant 与 算法的正确性

    在论述插入排序的正确性的时候, 书中引入了循环不变量的概念, 刚开始稍微有点不太明白, 早上查了一波资料之后决定把自己的理解记录下来. 什么是循环不变量 ? 在我看来, 所谓循环不变量的就是一个在循环 ...

  7. main与对象初始化 in C++

    没有学过代码编译的原理,以前也没有兴趣去学编译器的相关原理,但是近期通过阅读google开源项目gtest,对我稍有触动. 代码: main test示例 TEST宏定义 #define TEST(t ...

  8. libev 宏展开

    想看源码,宏太多,看着累,宏展开,再看,功力时间不够,先放下 放上宏展开后的代码. libev4.20 展开方示为 ./configure 修改makefile文件,字符串 替换CC为 CPP 注意要 ...

  9. MySQL的binlog数据如何查看

    binlog介绍 binlog,即二进制日志,它记录了数据库上的所有改变. 改变数据库的SQL语句执行结束时,将在binlog的末尾写入一条记录,同时通知语句解析器,语句执行完毕. binlog格式 ...

  10. 基于Jquery-ui的自动补全

    1.添加CSS和JS引用 <script type="text/javascript" src="javascript/jquery-1.7.min.js" ...