题目描述

给定一组不重叠的时间区间,在时间区间中插入一个新的时间区间(如果有重叠的话就合并区间)。
这些时间区间初始是根据它们的开始时间排序的。
示例1:
给定时间区间[1,3],[6,9],在这两个时间区间中插入时间区间[2,5],并将它与原有的时间区间合并,变成[1,5],[6,9].
示例2:
给定时间区间[1,2],[3,5],[6,7],[8,10],[12,16],在这些时间区间中插入时间区间[4,9],并将它与原有的时间区间合并,变成[1,2],[3,10],[12,16].
这是因为时间区间[4,9]覆盖了时间区间[3,5],[6,7],[8,10].

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].

链接: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的更多相关文章

  1. 【leetcode】Insert Interval

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  2. 60. Insert Interval && Merge Intervals

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  3. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

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

  4. leetcode Insert Interval 区间插入

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

  5. [OJ] Insert Interval

    LintCode #30. Insert Interval (Easy) LeetCode #57. Insert Interval (Hard) class Solution { public: v ...

  6. [Swift]LeetCode57. 插入区间 | Insert Interval

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

  7. 【LeetCode】57. Insert Interval

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  8. Leetcode: Merge/Insert Interval

    题目 Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[ ...

  9. [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals

    Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...

  10. 间隔问题,合并间隔(merge interval),插入间隔(insert interval)

    Merge Interval: Given a collection of intervals, merge all overlapping intervals. For example,Given  ...

随机推荐

  1. 098 01 Android 零基础入门 02 Java面向对象 03 综合案例(学生信息管理) 02 案例分析及实现 02 编写并测试Subject类

    098 01 Android 零基础入门 02 Java面向对象 03 综合案例(学生信息管理) 02 案例分析及实现 02 编写并测试Subject类 本文知识点:编写并测试Subject类 说明: ...

  2. ==38254==Sanitizer CHECK failed报错解决

    跑代码时发现有如下报错: LeakSanitizer: bad pointer 0x7ffd00735130==38254==Sanitizer CHECK failed: ../../../../l ...

  3. 不会吧,这也行?iOS后台锁屏监听摇一摇

    目录 背景介绍 探索过程 其他 APP 有没有类似功能 系统提供的摇一摇回调能否满足 其他方法能否实现 利用 CoreMotion 框架,监听加速计原始数据 通过加速计监听摇一摇 控制器相关逻辑和代码 ...

  4. JavaScript按钮排他思想

    要求: 有一系列按钮,要求每单击其中一个,该按钮改变样式(以背景颜色为例),其他按钮恢复保持默认样式. 实现思路: 获取所有按钮元素 首先先把其他按钮的背景颜色去掉 再单独设置自己的样式 代码实现: ...

  5. 结合实体框架(代码优先)、工作单元测试、Web API、ASP. net等,以存储库设计模式开发示例项目。NET MVC 5和引导

    介绍 这篇文章将帮助你理解在库模式.实体框架.Web API.SQL Server 2012.ASP中的工作单元测试的帮助下设计一个项目.净MVC应用程序.我们正在开发一个图书实体和作者专用的样例图书 ...

  6. 解决mvn clean install的报错The packaging for this project did not assign a file to the build artifact

    解决mvn clean install的报错The packaging for this project did not assign a file to the build artifact

  7. 多测师讲解python_os模块_高级讲师肖sir

    #os.path.isfile()#:判断当前是否为文件,返回布尔值是文件则True否者Falsea_path='F:\cms搭建.rar' #lesson包b_path=r'D:\bao\kk '# ...

  8. sop服务治理

    一,为什么需要服务治理: 我们最先接触的单体架构, 整个系统就只有一个工程, 打包往往是打成了 war 包, 然后部署到单一 tomcat 上面, 这种就是单体架构, 如图: 假如系统按照功能划分了, ...

  9. springMvc配置拦截器无效

    说明 springMvc配置国际化拦截器失败,点击页面按钮切换中英文无效,排查发现没有进入 LocaleChangeInterceptor 类中,判断拦截器没有起作用,那么是什么原因导致拦截器无效,通 ...

  10. 【差分】POJ 3263 Tallest Cow

    题目大意 POJ链接 给出\(n\)头牛的身高,和\(m\)对关系,表示牛\(a[i]\)与\(b[i]\)可以相互看见.已知最高的牛为第\(p\)头,身高为\(h\). 求每头牛的身高最大可能是多少 ...