leetcode93:insert-interval
题目描述
这些时间区间初始是根据它们的开始时间排序的。
示例1:
给定时间区间[1,3],[6,9],在这两个时间区间中插入时间区间[2,5],并将它与原有的时间区间合并,变成[1,5],[6,9].
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].
链接:https://www.nowcoder.com/questionTerminal/02418bfb82d64bf39cd76a2902db2190?f=discussion
来源:牛客网
class
Solution {
public
:
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
vector<Interval> res;
int
i=0;
for
(;i<intervals.size();i++){
if
(newInterval.end<intervals[i].start)
break
;
else
if
(newInterval.start>intervals[i].end)
res.push_back(intervals[i]);
else
{
newInterval.start=min(newInterval.start,intervals[i].start);
newInterval.end=max(newInterval.end,intervals[i].end);
}
}
res.push_back(newInterval);
for
(;i<intervals.size();i++)
res.push_back(intervals[i]);
return
res;
}
};
leetcode93:insert-interval的更多相关文章
- 【leetcode】Insert Interval
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- 60. Insert Interval && Merge Intervals
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- leetcode Insert Interval 区间插入
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Insert Interval 使用模拟 ...
- [OJ] Insert Interval
LintCode #30. Insert Interval (Easy) LeetCode #57. Insert Interval (Hard) class Solution { public: v ...
- [Swift]LeetCode57. 插入区间 | Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- 【LeetCode】57. Insert Interval
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- Leetcode: Merge/Insert Interval
题目 Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[ ...
- [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals
Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...
- 间隔问题,合并间隔(merge interval),插入间隔(insert interval)
Merge Interval: Given a collection of intervals, merge all overlapping intervals. For example,Given ...
随机推荐
- 怎么写一个Activity
a.新建一个类继承Actitvity b.重写oncreate方法 setContentView(R.layout.XXX);//设置布局文件 c.注册activity <activity an ...
- Recursive sequence (矩阵快速幂)2016ACM/ICPC亚洲区沈阳站
题目 Farmer John likes to play mathematics games with his N cows. Recently, they are attracted by recu ...
- Fedora version history --- kernel version
Fedora version history https://en.wikipedia.org/wiki/Fedora_version_history Version (Code name)[ ...
- 使用Python对植物大战僵尸学习研究
根据上一篇 使用Python读写游戏1 中,使用Python win32库,对一款游戏进行了读内存 操作. 今天来写一下对内存进行写的操作 正文 要进行32位的读写,首先了解一下要用到的几个函数,通过 ...
- MeteoInfoLab脚本示例:图形版面、点标注
在MeteoInfoLab界面中,图形的大小会随着它所在的窗口的大小改变而改变,在需要精确控制图中一些要素的位置的时候会比较困难,这时可以用figure函数的一些参数来控制图形版面大小.figure函 ...
- 分析如何直接绕过超时代VPY视频播放器的播放密码
声明:仅技术交流和学习! 前言: 你有没试过在网上下载一套视频,因网盘限速整整开机下载好几晚,下完后打开发现加密了,又找不到卖家注册.心里是几万只草泥马飞奔啊. 于是不甘心和好奇下,偿试自己破解. 目 ...
- day19 Pyhton学习 递归函数
# 函数的递归 : 在一个函数的内部调用它自己 # import sys # sys.setrecursionlimit(1000000) # 设置递归的最大深度 # 总结 # 1.递归函数的定义 : ...
- Token 、Cookie和Session的区别
本文转至http://blog.csdn.net/tobetheender/article/details/52485948 https://blog.csdn.net/axin66ok/articl ...
- gorm学习地址
1 gorm curd指南 2 gorm入门指南
- Django创建表时报错django.db.utils.InternalError: (1366问题解决记录
问题出现 执行Python manage.py makemigrations生成创建表的py文件 执行python manage.py migrate创建数据表 界面出现报错 问题原因 网上搜索原因, ...