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

思路:

Insert IntervalMerge Intervals的一个延伸问题,先看看怎么Merge

Given a collection of intervals, merge all overlapping intervals.

For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].

 vector<Interval> merge(vector<Interval> &intervals) {
if(intervals.size() <= )
return intervals; vector<Interval> vres;
sort(intervals.begin(), intervals.end(), intvalcomp);//先对interval排序
Interval tmp(intervals[]);
for(Interval it:intervals){
if(tmp.start == it.start){
tmp.end = it.end;
}else if(tmp.end >= it.start){//intervals有序,必然有tmp.start < it->start
if(tmp.end < it.end)//直接无视后者{[1,4],[2,3]}
tmp.end = it.end;//直接吞并后者{[1,3],[2,4]}
}else{
vres.push_back(tmp);//不相交
tmp = it;
}
}
vres.push_back(tmp);//漏掉这句会fail{[1,4],[1,4]}
return vres;
} bool intvalcomp(Interval a, Interval b){
if(a.start == b.start)
return a.end < b.end;
else
return a.start < b.start;
}

现在有了这个Merge好了的不相交区间序列,怎么进行插入呢?Insert Interval条件太多,每一个大小等号比较,每一个小下标就能让人栽跟斗,因此它也是我目前最讨厌的题目,没有之一。

一开始尝试这种思路:

“新序列按照start排好序(start肯定是各不相同的),第一步我们先用二分找出有交集的序列片段的开始,这一点很像Search Insert Position,然后再往后处理。”

脑子不清楚憋了一下午,恶心的我两天不能刷Leetcode,如果真要写出来的话,就老老实实下面这样,效率不一定差,因为看题目反正是不想要你改变输入参数,横竖都得遍历一遍来拷贝。挺有意思的是,晚上我看到了Google Campus的youku视频,讲述的就是一个倒霉孩子花了30min写二分Insert Interval的反例。。。

 vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
vector<Interval> rs;
int i = ;
while(i < intervals.size() && intervals[i].end < newInterval.start){//找到第一个起点
rs.push_back(intervals[i++]);
}
if(i == intervals.size()){//为空或过了结尾点
rs.push_back(newInterval);
return rs;
} newInterval.start = min(newInterval.start, intervals[i].start);
while(i < intervals.size() && intervals[i].start <= newInterval.end){//找到结束点
newInterval.end = max(newInterval.end, intervals[i++].end);
}
rs.push_back(newInterval); while(i < intervals.size()){
rs.push_back(intervals[i++]);
}
return rs;
}

【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals的更多相关文章

  1. 60. Insert Interval && Merge Intervals

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

  2. Insert Interval & Merge Intervals

    Insert Intervals Given a non-overlapping interval list which is sorted by start point. Insert a new ...

  3. leetcode Insert Interval 区间插入

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

  4. [LeetCode] Insert Interval 插入区间

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

  5. Leetcode 二分查找 Search Insert Position

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 T ...

  6. [leetcode]Insert Interval @ Python

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

  7. [LeetCode] Insert Interval 二分搜索

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

  8. Leetcode Insert Interval

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

  9. LeetCode OJ:Merge Intervals(合并区间)

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

随机推荐

  1. pycharm 导包

    如果需要requests包,python没有自带.可以执行命令:pip install requests,自动安装导入.

  2. ORACLE数据泵还原(IMPDP命令)【转】

      Oracle数据库还原IMPDP命令是相对于EXPDP命令的,方向是反向的.即对于数据库备份进行还原操作.一.知晓IMPDP命令 ? C:\>impdp -help Import: Rele ...

  3. CentOS 下的MySQL配置

    先贴出代码(/etc/my.cnf)如下: #The following options will be passed to all MySQL clients [client] #password ...

  4. 理解squid的正向和反向代理

    1.相同点: 访问的走向都是:客户端 -> 代理服务器 ->真实服务器 ->代理服务器->客户端 2.不同点:正向代理语义上更侧重于,让代理服务器去帮忙请求某个网址.让代理服务 ...

  5. CSS最常用和实用的技巧

    1.重置浏览器的字体大小重置浏览器的默认值 ,然后重设浏览器的字体大小你可以使用雅虎的用户界面重置的CSS方案 ,如果你不想下载9MB的文件,代码如下: body,div,dl,dt,dd,ul,ol ...

  6. 理解ROS rqt_console和 roslaunch

    1.使用rqt_console和roslaunch 这篇教程将介绍使用rqt_console和rqt_logger_level来调试以及使用roslaunch一次启动许多nodes.如果你使用ROS  ...

  7. HTML参考

    HTML Basic Document <html> <head> <title>Document name goes here</title> < ...

  8. windows8.1安装

    不小心下载了英文版的windows8.1的操作系统,要添加中文语言,结果遇到不少问题. 第一:安装中文语言包: 可以在控制面板-添加语言中添加,这个方法好像只能在线更新,那速度,不能忍.还可以下载离线 ...

  9. 模拟iOS系统原生导航条隐藏或显示动画

    借UIView动画,使更改导航条的hidden属性这一过程动起来.悦德财富:https://yuedecaifu.com 代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...

  10. UIViewController

    UIViewController 在MVC模式中就是C.关于MVC,可以看 UIViewController 主要具有什么功能呢? View Management When you define a ...