作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html

题目链接:leetcode Insert Interval

使用模拟的方法,把需要插入的区间和每一个给定的区间进行比较,有三种情况:

1.给定区间的起点小于要插入区间的终点,并且区间还未被查入过,那么插入区间。

2.给定区间的终点大于要插入区间的起点,或者插入区间已经被插入过了,那么插入给定区间。

3.不满足以上两种情况,说明给定区间与插入区间有交集,那么把需要插入的区间修改为其并集。

代码中的标记位inserted,标记需要插入的区间是否被插入到了结果中。

代码如下:

 /**
* 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;
bool inserted = ;
for( int i = ; i < intervals.size() ; i++ )
{
if( intervals[i].start > newInterval.end && inserted == )
{
res.push_back(newInterval);
res.push_back(intervals[i]);
inserted = ;
}
else if( intervals[i].end < newInterval.start || inserted== )
{
res.push_back(intervals[i]);
}
else
{
newInterval.start = min(newInterval.start, intervals[i].start);
newInterval.end = max(newInterval.end, intervals[i].end);
}
}
if( inserted == )
{
res.push_back(newInterval);
}
return res;
} };

leetcode Insert Interval 区间插入的更多相关文章

  1. [LeetCode] Insert Interval 插入区间

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

  2. [leetcode]Insert Interval @ Python

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

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

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

  5. [LeetCode] Insert Interval 二分搜索

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

  6. 57[LeetCode] Insert Interval

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

  7. [LeetCode] 57. Insert Interval 插入区间

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

  8. LeetCode 57. Insert Interval 插入区间 (C++/Java)

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

  9. leetCode 57.Insert Interval (插入区间) 解题思路和方法

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

随机推荐

  1. virtualbox 创建com对象失败

    其实这个错误是因为VirtualBox不兼容Win7引起的,只要把VirtualBox的兼容模式改为出Win7以外的就行了.. 右键VirtualBox的桌面快捷方式,选择属性,选到兼容性选项卡,勾选 ...

  2. 【原创】OllyDBG 入门系列(一)-认识OllyDBG

     ****** http://blog.fishc.com/645.html   标 题: [原创]OllyDBG 入门系列(一)-认识OllyDBG作 者: CCDebuger时 间: 2006-0 ...

  3. mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication的解决方法

    直接命令行操作没有问题,但是PHP连接就会报上面的错误. SET old_passwords =0; USE mysql; UPDATE user SET password =PASSWORD('yo ...

  4. Java正則表達式入门

     众所周知,在程序开发中,难免会遇到须要匹配.查找.替换.推断字符串的情况发生,而这些情况有时又比較复杂,假设用纯编码方式解决,往往会浪费程序猿的时间及精力.因此,学习及使用正則表達式,便成了解决这一 ...

  5. iOS-推送通知

    推送通知可以做3件事:(1)文字信息(2)一种声音 (3)一个徽章的标记号(第几条消息..) 推送通知流程  (app应用程序--->iOS 设备--->APNS(apple服务器)--- ...

  6. iOS开发——高级技术&支付宝功能的实现

    支付宝功能的实现   现在不少app内都集成了支付宝功能 使用支付宝进行一个完整的支付功能,大致有以下步骤: 1>先与支付宝签约,获得商户ID(partner)和账号ID(seller) (这个 ...

  7. AIR 移动设备上的存储控制

    File.documentsDirectory, File.userDirectory, File.desktopDirectory 等.可以保存大的数据,如图片,视屏,和临时文件.访问这些文件的全选 ...

  8. Debug Certificate expired on 11-5-8 上午 6:26

    好久没用电脑上的eclipse,今天新建了个安卓项目,发现报下面的错误:   后来得知: 是Debug证书过期所致. android应用必须经过签名证书进行数字签名后,才能安装,在开发调试阶段,默认情 ...

  9. Linux 学习笔记 基本的bash shell命令

    Linux 文件系统 Linux讲文件存储在单个目录结构(虚拟目录)中,虚拟目录包含了安装在PC上的所有存储设备的文件路径. Linux虚拟目录中比较复杂的部分是它如何来协调管理各个存储设备.Linu ...

  10. I2C总线协议的总结介绍

    在看天翔哥的视频之后,他强调要把I2C协议好好研究一下,那么就对一些基本的通信手段是十分有帮助的..那么就来了解一下I2C总线协议的一些知识吧. I2C(Inter-Integrated Circui ...