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 ...
随机推荐
- [hdu 4959]Poor Akagi 数论(卢卡斯数,二次域运算,等比数列求和)
Poor Akagi Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- Android应用开发揭秘之优化技术
2013-06-28 第15章 优化技术 不管用什么语言进行开发,所有的优秀代码都会展示出共有的经典品质: 简练,可读性强,模块化,层次性,设计良好,高效,优雅,清晰等. Java程序员能够依 ...
- 面面具到!android重力传感器
前两篇都是向大家介绍了很有意思的两种手势操作,嵌入我们游戏中,不得不说让游戏的自由度.可玩性和趣味性都增色不少!那么今天继续给大家介绍一亮点!传感器! 一:什么是传感器: 所谓传感器能够探测如光.热. ...
- iOS官方文档阅读 基本格式指北
一些关键词作用 NS_AVAILABLE 表示可用 如 NS_AVAILABLE(NA, 6_0);例如上面这句就是表示 该方法在6.0系统后可用 如果在6.0以下的系统用不了的 或者直接崩溃. NS ...
- jquery操作复选框(checkbox)十二技巧
jquery操作复选框(checkbox)的12个小技巧. 1.获取单个checkbox选中项(三种写法)$("input:checkbox:checked").val()或者$( ...
- linux 使用FIO测试磁盘iops 方法详解
FIO是测试IOPS的非常好的工具,用来对硬件进行压力测试和验证,支持13种不同的I/O引擎, 包括:sync,mmap, libaio, posixaio, SG v3, splice, null, ...
- Git Manual / Git使用手册 / Git, GitLab, Git Bash, TortoiseGit (建议全文复制到Word文档中通过导航窗格查看)
Git使用手册 目录 1 引言 2 Git.GitLab简介 2.1 Git 2.2 GitLab 2.3 Git基本概念 3 运行环境 4 ...
- vue-tree
vue-tree vue 编写的树形菜单,小巧实用,支持vue1.0,vue2.0 v1.0 功能: 1.支持多级树目录 2.支持高亮点击的节点 3.支持展开点击节点 4.支持点击收缩节点时收缩所有子 ...
- 客户端在向服务器的动态页发出请求的时候,服务器才会创建session
注意,纯的HTML页面很有可能导致服务器不会为用户创建session,即便是新到的用户也是如此. 在某些情况下也可能与服务器的具体配置有关系. 尤其是在对session进行监听的时候要注意这一点.
- oracle中空值null的判断和转换:NVL的用法
1.NULL空值概念 数据库里有一个很重要的概念:空值即NULL.有时表中,更确切的说是某些字段值,可能会出现空值, 这是因为这个数据不知道是什么值或根本就不存在. 2.NULL空值判断 空值不等同于 ...