leetcode Insert Interval 区间插入
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html
使用模拟的方法,把需要插入的区间和每一个给定的区间进行比较,有三种情况:
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 区间插入的更多相关文章
- [LeetCode] Insert Interval 插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [leetcode]Insert Interval @ Python
原题地址:https://oj.leetcode.com/problems/insert-interval/ 题意: Given a set of non-overlapping intervals, ...
- Leetcode Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [LeetCode]Insert Interval 考虑多种情况
写太复杂了. 思想:确定带插入区间的每一个边界位于给定区间中的哪个位置,共同拥有5种情况 -1 |(0)_1_(2)| (3) 当中.0,1,2这三种情况是一样的. 确定每一个带插入区间的两个边界分 ...
- [LeetCode] Insert Interval 二分搜索
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- 57[LeetCode] Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [LeetCode] 57. Insert Interval 插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- LeetCode 57. Insert Interval 插入区间 (C++/Java)
题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...
- leetCode 57.Insert Interval (插入区间) 解题思路和方法
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ...
随机推荐
- Foxmail
我们在“POP3/SMTP服务”前面打钩,这样我们的QQ邮箱设置已经完成了,我们就可以在foxmail客户端上QQ邮箱了 QQ邮箱的POP3与SMTP服务器是什么? QQ邮箱 POP3 和 SMTP ...
- string与数值之间的转换
9.50 编写程序处理一个vector<string>,其元素都表示整数型.计算vector中所有元素之和.修改程序,使之计算表示浮点值的string之和. 程序如下: #include& ...
- 学习笔记之APACHE ANT
http://baike.baidu.com/link?url=KkOWkH_nMVJRbd4oj-aIHMVL4HR-s7fqm3i2brUcZzBinwUXjZgzPcYIWz5qFNNvjait ...
- Google(谷歌)中国工程研究院 工程师 方坤 对学生朋友的一些建议
对学生朋友的一点建议 发表者:Google(谷歌)中国工程研究院工程师 方坤 自去年春天加入谷歌,我曾多次随公司校园招聘团队一起走访各地院校,帮助公司发掘人才 .利用这样的出差机会到处走走看看,饱览祖 ...
- Mysql-5.6乱码问题
1 参考:http://www.testwo.com/blog/6930 mysql数据库默认的编码是:Latin1,要想支持中文需要修改为gbk/utf8的编码格式. 1.以root管理员身份查 ...
- Hibernate学习笔记--------4.查询
一.Get/Load Get方法是立即检索,而load是延迟检索,他们都是根据主键进行查询.在<class>标签中,若把lazy属性改为false,load方法就会立即检索,class中的 ...
- git 撤销修改以及删除文件
撤销修改 1.如果当你修改了代码,然后又发现修改错误以后,想撤销前面的操作的时候该怎么办呢? 既然错误发现得很及时,就可以很容易地纠正它.你可以删掉最后一行,手动把文件恢复到上一个版本的状态.如果用 ...
- 微信、微博、qq图标服务实现
实现原理:变化前的图标和变化后的图标在一张图片上,用这张图片作为背景,通过定义背景的位置来实现显示哪个图标,其中还带着滑动的动画效果. <!DOCTYPE html> <html l ...
- 【linux操作命令】vim
高级一些的编辑器,都会包含宏功能,vim当然不能缺少了,在vim中使用宏是非常方便的: :qx 开始记录宏,并将结果存入寄存器x q 退出记录模式 @x 播放记录在x寄存器中的 ...
- php中GD库的一些简单使用
今天了解了一些GD库的简单使用,现在稍微做一下总结! GD库是什么?,graphic device,图像工具库,gd库是php处理图形的扩展库,gd库提供了一系列用来处理图片的API,使用GD库可以处 ...