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

题目的意思:给定一个非重叠的区间,插入一个新的区间,如果区间与其他区间相交,则必须合并

注意题目有个假设,区间根据start时间进行了排序,所以自己不需要排序

struct Interval {
int start;
int end;
Interval() : start(), end() {}
Interval(int s, int e) : start(s), end(e) {}
};

解题思路:设给定的区间为intervals,插入的区间为newInterval,只需要将newInterval与intervals相交的区间合并即可,

如果newInterval与intervals没有相交的区间,则必须将newInterval插入到相应的位置

本题有三种情况

  (1)如果intervals[i].end < newInterval.start,说明intervals[i]与newInterval不相交,保留即可

  (2)如果intervals[i].start > newInterval.end, 说明intervals[i]与newInterval不相交,将newInterval插入即可,注意这时候其后面的intervals[i],都可以保留,这是由于本身区间是不想交的。

   (3)  如果intervals[i].start < newInterval.end,说明区间相交,合并区间,更新newInterval

class Solution {
public:
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
vector<Interval> res;
for(int i = ; i < intervals.size(); ++ i){
if(intervals[i].end < newInterval.start) res.push_back(intervals[i]);
else if(intervals[i].start > newInterval.end) {
res.push_back(newInterval);
newInterval = intervals[i];
}else if(intervals[i].start <= newInterval.end ){
newInterval.start = min(newInterval.start, intervals[i].start);
newInterval.end = max(newInterval.end, intervals[i].end);
}
}
res.push_back(newInterval);
return res;
}
};

  

    

 

Leetcode Insert Interval的更多相关文章

  1. leetcode Insert Interval 区间插入

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

  2. [LeetCode] Insert Interval 插入区间

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

  3. [leetcode]Insert Interval @ Python

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

  4. 57[LeetCode] Insert Interval

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

  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 考虑多种情况

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

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

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

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

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

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

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

随机推荐

  1. centos ADSL 拨号上网设置

    下面主要介绍一下,在CentOS命令行环境下如何配置 ADSL 联网: 1.确保安装了网卡并能正常运行,使用命令查看一下网卡状态: [root@localhost simon]# /sbin/ifco ...

  2. 11月10日下午 ajax做显示信息以后用ajax、Bootstrp做弹窗显示信息详情

    1.用ajax做弹窗显示信息详情 nation.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&qu ...

  3. Google Maps API V3 之 图层

    Google官方教程: Google 地图 API V3 使用入门 Google 地图 API V3 针对移动设备进行开发 Google 地图 API V3 之事件 Google 地图 API V3 ...

  4. 2014 Multi-University Training Contest 9#1009

    Just a JokeTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Tot ...

  5. PHP安装模式cgi、fastcgi、php_mod比较

    先了解一下普通cgi的工作流程: web server收到用户请求,并把请求提交给cgi程序,cgi程序根据请求提交的参数作相应处理,然后输出标准的html语句返回给web server,web se ...

  6. PHP写文件函数

    /** * 写文件函数 * * @param string $filename 文件名 * @param string $text 要写入的文本字符串 * @param string $openmod ...

  7. PHP函数 addslashes() 和 mysql_real_escape_string() 的区别 && SQL宽字节,绕过单引号注入攻击

    首先:不要使用 mysql_escape_string(),它已被弃用,请使用 mysql_real_escape_string() 代替它. mysql_real_escape_string() 和 ...

  8. 【Android学习】android:layout_weight的用法实例

    对于android:layout_weight的用法,用下面的例子来说明: <LinearLayout xmlns:android="http://schemas.android.co ...

  9. Java类名.class和getClass()区别

    区别 类名.class叫做“类字面量”,因class是关键字, 所以类名.class编译时确定. getclass()运行时根据实际实例确定,getClass()是动态而且是final的. Strin ...

  10. C++基础知识(3)---new 和 delete

    学过c语言的人都知道,c语言中动态分配内存空间使用的是库函数malloc,calloc,realloc以及free.而c++中所使用的是关键字new和delete.如 动态分配 new  ,  撤销内 ...