Leetcode: Merge/Insert Interval
题目
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]
.
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
You may assume that the intervals were initially sorted according to their start times.
Example 1:
Given intervals [1,3],[6,9]
, insert and merge [2,5]
in as [1,5],[6,9]
.
Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16]
, insert and merge [4,9]
in as [1,2],[3,10],[12,16]
.
This is because the new interval [4,9]
overlaps with [3,5],[6,7],[8,10]
.
bool sortInterval(const Interval &i1, const Interval &i2) {
return i1.start < i2.start;
}
class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
vector<Interval> newVec;
if(intervals.size() <= 0)
return newVec; sort(intervals.begin(), intervals.end(), sortInterval);
for(int i = 0; i < intervals.size(); i ++) {
Interval nextInt = intervals[i];
if(i == intervals.size()-1) {
newVec.push_back(nextInt);
break;
}
Interval tmp = intervals[i+1];
while(tmp.start <= nextInt.end) {
nextInt.end = max(tmp.end, nextInt.end);
i++;
if(i+1<intervals.size())
tmp = intervals[i+1];
else {
newVec.push_back(nextInt);
return newVec;
}
}
newVec.push_back(nextInt);
}
return newVec;
}
};
Leetcode: Merge/Insert Interval的更多相关文章
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- leetcode 57 Insert Interval & leetcode 1046 Last Stone Weight & leetcode 1047 Remove All Adjacent Duplicates in String & leetcode 56 Merge Interval
lc57 Insert Interval 仔细分析题目,发现我们只需要处理那些与插入interval重叠的interval即可,换句话说,那些end早于插入start以及start晚于插入end的in ...
- 【leetcode】Insert Interval
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- leetCode 57.Insert Interval (插入区间) 解题思路和方法
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ...
- 第一周 Leetcode 57. Insert Interval (HARD)
Insert interval 题意简述:给定若干个数轴上的闭区间,保证互不重合且有序,要求插入一个新的区间,并返回新的区间集合,保证有序且互不重合. 只想到了一个线性的解法,所有区间端点,只要被其 ...
- Java for LeetCode 057 Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [LeetCode] 57. Insert Interval 插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- LeetCode 57. Insert Interval 插入区间 (C++/Java)
题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...
- [Leetcode][JAVA] Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
随机推荐
- Hadoop集群+Spark集群搭建(一篇文章就够了)
本文档环境基于ubuntu16.04版本,(转发请注明出处:http://www.cnblogs.com/zhangyongli2011/ 如发现有错,请留言,谢谢) 一.准备 1.1 软件版本 Ub ...
- 【LeetCode】77. Combinations (2 solutions)
Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ... ...
- mysql分区 详解
第18章:分区 目录 18.1. MySQL中的分区概述 18.2. 分区类型 18.2.1. RANGE分区 18.2.2. LIST分区 18.2.3. HASH分区 18.2.4. KEY分区 ...
- systemctl 配置mysql 开机启动
在centos 7 环境下对服务的管理已经不再用service 命令了,而是改为systemctl 命令来管理服务. 一.创建systemctl 的对mysql服务的配置文件: touch /usr/ ...
- AngularJS 中的作用域
问题引入 使用 Angular 进行过一段时间的开发后,基本上都会遇到一个这样的坑: 123456789101112 <div ng-controller="TestCtrl" ...
- Nmap速查手册
http://drops.wooyun.org/tips/4333 From:http://highon.coffee/docs/nmap/ 0x00:说明 只是一个快速查询手册,理论的东西都没有补充 ...
- OS开发UI篇—UIWindow简单介绍
一.简单介绍 UIWindow是一种特殊的UIView,通常在一个app中只会有一个UIWindow iOS程序启动完毕后,创建的第一个视图控件就是UIWindow,接着创建控制器的view,最后将控 ...
- Mockjs生成Vue数据表格
1.npm install mockjs --save 2.在src文件下建mock.js文件 3.mock.js文件文件内容 //引入mockjs import Mock from 'mockjs' ...
- ubuntu和pypi换源
ubuntu用apt-get下载的源是可以更换的.之前一直是打开软件中心在编辑里找源,找到后系统会自动备份原来的源并换源.奇怪却搜不到自己学校的源=.= 想换源还有一个原因,之前在update的时候会 ...
- 使用Crypto++库的CBC模式实现加密(二)
前面已经有一篇介绍使用Crypto++库实现的加密的文章了,但是代码中考虑的不完全,所以就重新发了个二 C++封装: #include "zyaes.h" #include < ...