【Leetcode】【Hard】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]
.
解题思路:
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的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【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 ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 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 ...
- 【LeetCode每天一题】Merge Intervals(合并区间)
Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...
- 【leetcode刷题笔记】Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 【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 ...
- 【LeetCode每天一题】 Merge k Sorted Lists(合并K个有序链表)
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...
- 【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 ...
随机推荐
- 2016年 Delphi Roadmap
2016年delphi Roadmap 发布,这也是新公司的第一次发布路线图. 虽然稍微晚点( 原来说是1月份发布路线图),至少比过去积极点.喧嚣多年的靴子终于落地. Linux 的支持终于正式公布. ...
- MySQL 5.7 深度解析: 半同步复制技术
复制架构衍生史 在谈这个特性之前,我们先来看看MySQL的复制架构衍生史. MySQL的复制分为四种: 普通的replication,异步同步. 搭建简单,使用非常广泛,从mysql诞生之初,就产生了 ...
- Python 2.7_First_try_爬取阳光电影网_20161206
之前看过用Scrapy 框架建立项目爬取 网页解析时候用的Xpath进行解析的网页元素 这次尝试用select方法匹配元素 1.入口爬取页面 http://www.ygdy8.com/index.ht ...
- {vlFeat}{Matlab}Linux中matlab的vlFeat配置
1.下载vlFeat编译后的版本binary package 2.解压后将 toolbox/,bin/,data/ 等文件夹复制到matlab新建工具箱目录 /toolbox/vlfeat/ 中 3. ...
- Add Binary <leetcode>
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- SQL Server 查询处理中的各个阶段(SQL执行顺序)
SQL 不同于与其他编程语言的最明显特征是处理代码的顺序.在大数编程语言中,代码按编码顺序被处理,但是在SQL语言中,第一个被处理的子句是FROM子句,尽管SELECT语句第一个出现,但是几乎总是最后 ...
- flex 导出Excel功能实现
方法一: 1.Excel导出主要代码: try { var bytes: ByteArray = new ByteArray(); bytes.writeMultiByte(DataG ...
- 循序渐进Python3(三) -- 0 -- 初识函数
函数 如果我们要计算一个圆的面积,就需要知道它的半径,然后根据公式S=3.14*r*r算出它的面积,如果我们要算100个圆的面积,则每次我们都需要写公式去计算,是不是很麻烦,但是有了函数的话,我们就不 ...
- Echarts x轴显示不全
xAxis : [ { type : 'category', data : ['采矿业','制造业','电力热力燃气及水生产和供应业','建筑业'], axisTick: { alignWithLab ...
- sublimetext
下载地址:http://www.sublimetext.com/ 详情:http://baike.baidu.com/link?url=uoObJWXyy_-zu52HuOKzfKuwHEpL2JQn ...