【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 ...
随机推荐
- [转]分享一个用Telnet代替JLinkRTTClient的办法,实现同时显示和记录
原帖子http://www.amobbs.com/thread-5614514-1-1.html Jlink 驱动 升级到4.96后,提供了一个 JLinkRTTViewer,确实方便了很多,但似乎不 ...
- 一个前辈对FPGA的理解
接下来对比一下我原来和现在对于FPG A的认识:原来从单片机转型到FPG A时,并没有摸清这趟河水的深浅,而在不知深浅的情况下,我已经开始下水了.当时我认为FPG A和单片机一样,它是由一个超级经典的 ...
- 关于生物信息学与R的相关资料和网站
生物信息学的相关论坛:http://www.omicshare.com/forum/ 糗世界:http://qiubio.com:8080/ 统计之都网站 绘制QQ图和曼哈顿图:http://www. ...
- [原创]Matlab获取当前时间信息
本文主要介绍下Matlab中如何获取当前时间的一些方法. 基本变量date.now.clock date 按照日期字符串返回当前系统时间 now 按照连续的日期数值返回当前系统时间 clock按照日期 ...
- 第一章 tomcat安装与启动
一.安装 1.下载tomcat安装包 2.解压安装包 3.配置环境变量 打开~/.bash_profile文件,输入一下两句话: export TOMCAT_HOME=/Users/enniu1/De ...
- PHP各种缓存
缓存类型PHP缓存包括PHP变异缓存和PHP数据缓存两种:PHP编译缓存: PHP数据缓存: 对数据库数据进行缓存 对PHP模板数据缓存 js数据缓存
- Android开发涉及有点概念&相关知识点(待写)
前言,承接之前的 IOS开发涉及有点概念&相关知识点,这次归纳的是Android开发相关,好废话不说了.. 先声明下,Android开发涉及概念比IOS杂很多,可能有很多都题不到的.. 首先由 ...
- JProfiler使用入门(一)——准备工作
JProfiler是一个全功能的Java剖析工具(profiler),主要用于检查和跟踪系统(限于Java开发的)的性能. JProfiler可以通过时时的监控系统的内存使用情况,随时监视垃圾回收 ...
- redis队列的实现
redis中文官网:http://www.redis.cn/ 关于redis队列的实现方式有两种: 1.生产者消费者模式. 2.发布者订阅者模式. 详解: 1.生产者消费者模式. 普通版本: 比如一个 ...
- NGINX下配置404错误页面的方法分享
NGINX下配置自定义的404页面是可行的,而且很简单,只需如下几步,需要的朋友可以参考下 1. 创建自己的404.html页面 2.更改nginx.conf在http定义区域加入: fastcg ...