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 ...
随机推荐
- getopt函数用法
getopt被用来解析命令行选项参数. #include <unistd.h> extern char *optarg; //选项的参数指针 extern int o ...
- day64:nginx模块之限制连接&状态监控&Location/用nginx+php跑项目/扩展应用节点
目录 1.nginx模块:限制连接 limit_conn 2.nginx模块:状态监控 stub_status 3.nginx模块:Location 4.用nginx+php跑wordpress项目 ...
- Python+Appium自动化测试(15)-使用Android模拟器(详细)
做APP的UI自动化测试时,我们往往会使用真机跑自动化测试脚本,因为这样才是最真实的使用场景.但前期调试脚本的话,可以先使用模拟器,这样相对更加方便. 不推荐使用Android SDK里自带模拟器,太 ...
- 如何win7安装tomcat
首先安装jdk,查看当前jdk版本. >java -version 显示的是1.8.0_131 为此我们安装的tomcat版本是apache-tomcat-9.0.38-windows-x64. ...
- C#实现迭代器
迭代器模式(Iterator),提供一种方法顺序访问一个聚合对象中的各种元素,而又不暴露该对象的内部表示.C#中使用IEnumerator接口实现,Java中使用Iterator接口实现,其中原理都差 ...
- php超全局数组 为什么swoole的http服务不能用
php的超全局数组$_GET等九个 可以直接使用 无需定义 实际上是浏览器请求到Apache或者nginx的时候 转发到PHP处理模块 fpm转发给php解释器处理 php封装好后丢给php的 sw ...
- 如何使用 Gin 和 Gorm 搭建一个简单的 API 服务 (二)
创建 API 我们之前已经跑过 Gin 框架的代码,现在是时候加些功能进去了. 读取全部信息 我们先从"增删改查"中的"查"入手,查询我们之前添加的信息.我接下来要删除几行代码,并把 Gin ...
- composer慢 设置阿里云镜像
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer
- __getattr__和__setattr__
getattr 拦截运算(obj.xx),对没有定义的属性名和实例,会用属性名作为字符串调用这个方法 class F(object): def __init__(self): self.name = ...
- JS XMLHttpRequest请求
前言 我们知道jq的请求非常简短好用,但是其实js原生的请求也不差,并且不用插件更能说明自己本身的技术已经很强了,别人看自己代码一脸懵逼的时候,这时就可以一一解释这些代码的用处,更能让别人敬佩! JS ...