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:

Input: intervals = [[1,3],[6,9]], newInterval = [2,5]
Output: [[1,5],[6,9]]

Example 2:

Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
Output: [[1,2],[3,10],[12,16]]
Explanation: Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10].


/**
* 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:
vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {
vector<Interval> res;
int index = ;
while(index < intervals.size() && intervals[index].end < newInterval.start){
res.push_back(intervals[index++]);
}
while(index < intervals.size() && intervals[index].start <= newInterval.end){
newInterval.start = min(newInterval.start, intervals[index].start);
newInterval.end = max(newInterval.end, intervals[index].end);
index++;
}
res.push_back(newInterval);
while(index < intervals.size()){
res.push_back(intervals[index++]);
}
return res;
}
};



57[LeetCode] Insert Interval的更多相关文章

  1. leetcode Insert Interval 区间插入

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Insert Interval 使用模拟 ...

  2. LeetCode(57) Insert Interval

    题目 Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nece ...

  3. [LeetCode] Insert Interval 插入区间

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  4. [leetcode]Insert Interval @ Python

    原题地址:https://oj.leetcode.com/problems/insert-interval/ 题意: Given a set of non-overlapping intervals, ...

  5. Leetcode Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  6. [LeetCode] Insert Interval 二分搜索

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  7. [LeetCode]Insert Interval 考虑多种情况

    写太复杂了. 思想:确定带插入区间的每一个边界位于给定区间中的哪个位置,共同拥有5种情况 -1 |(0)_1_(2)|  (3) 当中.0,1,2这三种情况是一样的. 确定每一个带插入区间的两个边界分 ...

  8. 【LeetCode】57. Insert Interval [Interval 系列]

    LeetCode中,有很多关于一组interval的问题.大体可分为两类: 1.查看是否有区间重叠: 2.合并重叠区间;  3.插入新的区间: 4. 基于interval的其他问题 [ 做题通用的关键 ...

  9. leetcode 56. Merge Intervals 、57. Insert Interval

    56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...

随机推荐

  1. Oracle作业5——多表查询、子查询

    一.基础练习: 1.查询和scott相同部门的员工姓名ename和雇用日期hiredate SELECT ENAME,HIREDATE FROM EMP WHERE DEPTNO=(SELECT DE ...

  2. CGAffineTransform 视频旋转(转)

    记录下视频旋转 ////////////////////////////////////////////// - (void)test:(NSURL *)url transformUrl:(NSURL ...

  3. GPUImage源码解读之GPUImageContext

    GPUImageContext类,提供OpenGL ES基本上下文,GPUImage相关处理线程,GLProgram缓存.帧缓存.由于是上下文对象,因此该模块提供的更多是存取.设置相关的方法. 属性列 ...

  4. python多线程知识-实用实例

    python多线程使用场景:IO操作,不适合CPU密集操作型任务   1.多个线程内存共享 2.线程同时修改同一份数据需要加锁,mutex互斥锁 3.递归锁:多把锁,锁中有锁 4.python多线程, ...

  5. <CPP学习 第二天> 字符串的输入 及 String类

    今天简单的学习了字符串的输入以及C++的String类. 1.面向行的输入: getline(); getline()函数读取整行,通过回车键输入的换行符来确定输入结尾.要调用这种方法,可以使用cin ...

  6. Codeforces Round #483 (Div. 2)C题

    C. Finite or not? time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  7. 缓存反向代理-Varnish

    简介 Varnish是一款高性能.开源的缓存反向代理服务器.它从客户端接受请求,并尝试从缓存中响应请求,如果无法从缓存中提供响应,Varnish 向后端服务器发起请求,获取响应,将响应存储在缓存中,然 ...

  8. 慎使用sql的enum字段类型

    在sql的优化中,会有同学提到一点:使用enum字段类型,代替其他tinyint等类型.以前这也是不少人喜欢优化的,但是现在细想,是非常不合理的. 优点: 1.可以设置区间范围,比如设置性别:1男2女 ...

  9. sample采样倾斜key并单独进行join代码

    /** * sample采样倾斜key单独进行join */ JavaPairRDD<Long, String> sampledRDD = userid2PartAggrInfoRDD.s ...

  10. 二、linux编译环境的搭建

    1.linux编译工具安装 vim安装:apt-get install vim 注意:使用C语言源代码语法加亮功能,需要配置文件/etc/vim/vimrc,加入代码syntaxon.文件后缀必须为. ...