Leetcode Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
You may assume that the intervals were initially sorted according to their start times.
Example 1:
Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].
Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].
This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10].
题目的意思:给定一个非重叠的区间,插入一个新的区间,如果区间与其他区间相交,则必须合并
注意题目有个假设,区间根据start时间进行了排序,所以自己不需要排序
struct Interval {
int start;
int end;
Interval() : start(), end() {}
Interval(int s, int e) : start(s), end(e) {}
};
解题思路:设给定的区间为intervals,插入的区间为newInterval,只需要将newInterval与intervals相交的区间合并即可,
如果newInterval与intervals没有相交的区间,则必须将newInterval插入到相应的位置
本题有三种情况
(1)如果intervals[i].end < newInterval.start,说明intervals[i]与newInterval不相交,保留即可
(2)如果intervals[i].start > newInterval.end, 说明intervals[i]与newInterval不相交,将newInterval插入即可,注意这时候其后面的intervals[i],都可以保留,这是由于本身区间是不想交的。
(3) 如果intervals[i].start < newInterval.end,说明区间相交,合并区间,更新newInterval
class Solution {
public:
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
vector<Interval> res;
for(int i = ; i < intervals.size(); ++ i){
if(intervals[i].end < newInterval.start) res.push_back(intervals[i]);
else if(intervals[i].start > newInterval.end) {
res.push_back(newInterval);
newInterval = intervals[i];
}else if(intervals[i].start <= newInterval.end ){
newInterval.start = min(newInterval.start, intervals[i].start);
newInterval.end = max(newInterval.end, intervals[i].end);
}
}
res.push_back(newInterval);
return res;
}
};
Leetcode Insert Interval的更多相关文章
- leetcode Insert Interval 区间插入
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接: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, ...
- 57[LeetCode] Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [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 & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals
Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...
- 【LeetCode】57. Insert Interval [Interval 系列]
LeetCode中,有很多关于一组interval的问题.大体可分为两类: 1.查看是否有区间重叠: 2.合并重叠区间; 3.插入新的区间: 4. 基于interval的其他问题 [ 做题通用的关键 ...
随机推荐
- Hibernate映射文件如何配置触发器
Hibernate映射文件之触发器生成(generated属性.database-object元素) (2013-02-27 12:28:49) 转载▼ 标签: it 分类: JAVA学习笔记 这里分 ...
- linux用命令删除重复行
文本处理时,经常要删除重复行,下面是三种方法 第一,用sort+uniq,注意,单纯uniq是不行的. sort -n test.txt | uniq 第二,用sort+awk命令,注意,单纯awk同 ...
- [nosql之redis]yum安装redis
1.首先对于这种nosql来说目前我用到的功能很少,所以感觉没有必要去优化他跟不需要去编译安装.今天来介绍下一个yum安装redis 步骤1:安装扩展yum库 [root@localhost ~]# ...
- UI第十一节——UIActivityIndicatorView
- (void)viewDidLoad { [super viewDidLoad]; // 创建一个UIActivityIndicatorView,大小是固定的 UIActi ...
- 第2月第25天 BlocksKit
1.blockskit https://github.com/zwaldowski/BlocksKit bk_showAlertViewWithTitle 2.toast +(void)showToa ...
- 计算 TP90TP99TP...
what-do-we-mean-by-top-percentile-or-tp-based-latency tp90 is a minimum time under which 90% of requ ...
- linux常用命令-文件搜索命令-locate,which,whereis,grep
locate 目录或文件名 -i 查找的时候不区分大小写 这个类似everything,速度比find快很多,因为这个命令搜索的是它维护的文件资料库,文件资料库是var/lib/mlocate/mlo ...
- [codevs1105][COJ0183][NOIP2005]过河
[codevs1105][COJ0183][NOIP2005]过河 试题描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青 ...
- Android 学习笔记
1.sleep(),wait(),notify(),notifyAll() sleep()是线程类的静态方法,阻塞线程一定时间后再次使线程处于可以被调度运行的状态wait(),notify(),not ...
- python 字典的函数
clear(),清空 注意单纯的赋值就相当于c语言中引用,只事额外起了一个别名,所以他们指向相同的地址, 所以令c={},只是另外开辟了一个新的空间让c为空,并没有改变之前的空间,所以{}与clear ...