作者: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. c/c++中使用指针需要注意的问题

    一.使用指针的时候需要注意几点: 分配空间 初始化 释放 二.常见的错误有几种: 1)内存分配未成功,却使用了它      编程新手常犯这种错误,因为他们没有意识到内存分配会不成功.常用解决办法是,使 ...

  2. 简单详细的OD破解教程

    2007-08-04 15:46作者:CCDebuger注:昨天在网上见到了这篇文章,但缺少插图,从另外一篇文章中也看到了类似的的教程文章,里面的插图质量实在不敢恭维.在一个论坛中正好下载了文章中所介 ...

  3. android常用的快捷键

    Ctrl + shift + O    删除.java文件中所有未用到的引用的包的快捷键 Ctrl+D 删除光标所在行 Ctrl + shift + F   代码整体对齐:如果失效的情况下,就选中代码 ...

  4. BZOJ 4291: [PA2015]Kieszonkowe 水题

    4291: [PA2015]Kieszonkowe Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnli ...

  5. javax.naming.NameNotFoundException:Name[ XXX] is not bound in this context.

    在用局部数据源去连数据库的时候,在本地的项目中,都是可以的,可是一部署到服务器上,就报错了. 报的错误是:javax.naming.NameNotFoundException:Name[ XXX] i ...

  6. 基于Android 平台简易即时通讯的研究与设计[转]

    摘要:论文简单介绍Android 平台的特性,主要阐述了基于Android 平台简易即时通讯(IM)的作用和功能以及实现方法.(复杂的通讯如引入视频音频等可以考虑AnyChat SDK~)关键词:An ...

  7. 面试题总结之Linux/Shell

    Linux Linux cshrc文件作用 Linux如何起进程/查看进程/杀进程 Linux 文件755 代表什么权限 Linux辅助线程 Linux进程间通信方法 pipeline,msgq... ...

  8. C#_MVC 自定义AuthorizeAttribute实现权限管理

    随笔- 28  文章- 31  评论- 16 MVC 自定义AuthorizeAttribute实现权限管理   在上一节中提到可以使用AuthorizeAttribute进行权限管理: [Autho ...

  9. java_泛型,设置类型通配符的上限

    package ming; import java.util.ArrayList; import java.util.Collection; import java.util.List; class ...

  10. Asp.Net 之 获取GridView行的DataKeys

    1.后台任意一个位置获取GridView行的主键值 foreach (GridViewRow row in GridView1.Rows) { string order_Id=this.GridVie ...