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 ...
随机推荐
- 安装Intel HAXM为Android 模拟器加速,30秒内启动完成
要求 必备知识 windows 7 基本操作. 运行环境 windows 7(64位); Android Studio 1.1.0;JDK 1.7.0_75(64位);android-sdk_r24 ...
- 【Java】推断文件的后缀名
这本来不是一个问题,利用框架本来有的方法.或者File类的getPath()方法,取出要推断文件路径.或者getName()方法取出文件路径,成为一个String字符串如果为fileName之后,再对 ...
- Windows下python环境变量配置
默认情况下,在windows下安装python之后,系统并不会自动添加相应的环境变量.此时不能在命令行直接使用python命令. 1. 首先需要在系统中注册python环境变量:假设python的安装 ...
- C#_dropdownlist_1
关于ASP.net MVC 中DropDownList绑定与提交数据 在做 ASP.net MVC项目中,数据绑定也是很关键的,现在以个人经验与大家交流下 ASP.net MVC 中DropDow ...
- 【转】C++ char数组转化为string
有很多种方法: 假设c字符串定义为char ch[]="hello world!"; 1.向构造函数传入c字符串创建string对象: string str(ch); 2.使用拷贝 ...
- unicode 编码总结
unicode简介: unicode又称为unicode character set,缩写为ucs,意为字符集.编码方式有utf-7,utf-8,utf-16,utf-32几种,常用的是utf-8和u ...
- Encryption
Encryption Configuration Basic Usage Encrypting a value Decrypting a value Configuration Before usin ...
- Android中的事件分发机制总结
Android 的事件分发机制 一.View的事件分发总结: View的onTouchEvent和OnTouch区别 还是以自定义的TestButton为例. 我们可以通过重写onTouchEven ...
- PHP微信开发ReplyModel(封装验证,数据获取,信息返回)
<?phpclass ReplyModel{ //验证token, public function ValidationToken($token){ if(isset($_GET["e ...
- jquery checkbox全选,全不选,反选方法,jquery checkbox全选只能操作一次
jquery checkbox全选,全不选,反选方法, jquery checkbox全选只能操作一次, jquery checkbox全选只有第一次成功 >>>>>&g ...