作者: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. SQL Server磁盘I/O性能分析

    SQL Server中的I/O操作类型: 1.对于内存中没有缓存的数据,第一次访问时需要将数据从所在的页面从数据文件中读取到内存中 2.在任何Insert/Update/Delete提交前,SQL S ...

  2. [React Native] Using the Image component and reusable styles

    Let's take a look at the basics of using React Native's Image component, as well as adding some reus ...

  3. 基于反射实现自己主动化restful开发

    [Author]: kwu 基于反射实现自己主动化restful开发,通用的仅仅须要写查询数据库的sql.并增加相应的javabean实现的高速restful服务的开发. 1.编写数据库的查询sql. ...

  4. [xml解析]rapidxml读取文件

    因为项目需要读取xml配置文件,在原来调查一番后,项目组使用了tinyxml. tinyxml确实简单,非常清楚的就把读取方案写出来了.但是,由于后期xml文件越来越大(2.5M,大概1w多行数据), ...

  5. vmware mac虚拟机 停在启动界面

    前言: 关于vmware安装mac 10.8 可以从参看:http://www.cnblogs.com/zyf2013/p/3888242.html 安装完成以后重新启动,卡在了白苹果启动画面处. 或 ...

  6. ADT在线安装

    以往安装ADT根本就不是个问题,可是现在就是个大问题了,联通的宽带网络连www.google.com.hk都打不开,你叫我们这些P民怎么活? https://dl-ssl.google.com/and ...

  7. ubuntu 13.04 root权限设置方法详解

    很多朋友安装升级Ubuntu 13.04之后不知道ubuntu 13.04 root权限设置的具体方法,今天这篇文章就将为大家详细介绍设置root权限的步骤,新手朋友可以来看一看哦~ Ubunto 1 ...

  8. Android(java)学习笔记86:案例短信发送器

    1.一般我们第一步都是先创建这个main.xml布局文件,这是良好的习惯: <?xml version="1.0" encoding="utf-8"?&g ...

  9. 解析搜狗词库(python)

    #!/usr/bin/python # -*- coding: utf-8 -*- import struct import sys import binascii import pdb #搜狗的sc ...

  10. VS版本下载

    tfs2012:BVGTF-T7MVR-TP46H-9Q97G-XBXRB http://www.microsoft.com/zh-cn/download/details.aspx?id=30658 ...