57. Insert Interval (Array; Sort)
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].
思路:和上一题差不多,但要考虑更多问题,注意不要漏掉newInterval可能整体插入(不做merge)的情况
/**
* 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) {
if(intervals.empty()){
intervals.push_back(newInterval);
return intervals;
} //first find the fist end >= newInterval.start
vector<Interval>::iterator startInsertPos = intervals.begin();
for(; startInsertPos < intervals.end(); startInsertPos++){
if(startInsertPos->end >= newInterval.start) break;
}
if(startInsertPos == intervals.end()){ //insert in the final
intervals.push_back(newInterval);
return intervals;
} //find the last start <= newInterval.end
vector<Interval>::iterator endInsertPos = startInsertPos;
for(; endInsertPos < intervals.end(); endInsertPos++){
if(endInsertPos->start > newInterval.end){
break;
}
}
endInsertPos--; //intervals between [startInsertPos, endInsertPos] may need to be merged
//case 1: insert before startInsertPos
if(startInsertPos->start > newInterval.end){
intervals.insert(startInsertPos,newInterval);//insert in the position startInserPos
}
//case2: insert after endInsertPos
else if(endInsertPos->end < newInterval.start){
intervals.insert(endInsertPos+,newInterval);//insert in the position endInsertPos+1
}
//case3: merge
else{
startInsertPos->start = min(newInterval.start, startInsertPos->start);
startInsertPos->end = max(newInterval.end, endInsertPos->end);
intervals.erase(startInsertPos+,endInsertPos+);//erase the elem from startInserPos+1 to endInsertPos
}
return intervals;
57. Insert Interval (Array; Sort)的更多相关文章
- 【LeetCode】57. Insert Interval [Interval 系列]
LeetCode中,有很多关于一组interval的问题.大体可分为两类: 1.查看是否有区间重叠: 2.合并重叠区间; 3.插入新的区间: 4. 基于interval的其他问题 [ 做题通用的关键 ...
- leetcode 56. Merge Intervals 、57. Insert Interval
56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...
- 56. Merge Intervals 57. Insert Interval *HARD*
1. Merge Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[ ...
- leetcode 57 Insert Interval & leetcode 1046 Last Stone Weight & leetcode 1047 Remove All Adjacent Duplicates in String & leetcode 56 Merge Interval
lc57 Insert Interval 仔细分析题目,发现我们只需要处理那些与插入interval重叠的interval即可,换句话说,那些end早于插入start以及start晚于插入end的in ...
- 【LeetCode】57. Insert Interval
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- leetCode 57.Insert Interval (插入区间) 解题思路和方法
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ...
- 第一周 Leetcode 57. Insert Interval (HARD)
Insert interval 题意简述:给定若干个数轴上的闭区间,保证互不重合且有序,要求插入一个新的区间,并返回新的区间集合,保证有序且互不重合. 只想到了一个线性的解法,所有区间端点,只要被其 ...
- [leetcode sort]57. Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- 57. Insert Interval
题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...
随机推荐
- hdu 1278 逃离迷宫
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 1085 Perfect Sequence (25 分)
1085 Perfect Sequence (25 分) Given a sequence of positive integers and another positive integer p. T ...
- [UE4]代理事件(C++)
用宏定义类似格式: DECLARE_DELEGATE //普通代理 DECLARE_DYNAMIC_DELEGATE_TwoParams //动态代理 DECLARE_DYNAMIC_MULTICAS ...
- Unreal Engine 4 笔记
1.UE4的调试输出 //*1 调试输出*// /*case a.快速使用 不设置log类别 默认为LogTemp*/ UE_LOG(LogTemp,Log,TEXT("Your messa ...
- Maven 配置tomcat插件
使用tomcat插件来访问maven 1 先下载tomcat插件(在pom中配置) <!-- 配置Tomcat插件 --> <plugin> <groupId>or ...
- spark1.0属性配置以及spark-submit简单使用
在spark1.0中属性支持三种配置方式: 1.代码 在代码中构造SparkConf时指定master.appname或者key-value等 val conf = new SparkConf(); ...
- Redis如何存储对象与集合示例详解
前言 大家都知道在项目中,缓存以及mq消息队列可以说是不可或缺的2个重要技术.前者主要是为了减轻数据库压力,大幅度提升性能.后者主要是为了提高用户的体验度,我理解的是再后端做的一个ajax请求(异 ...
- python入门-WHILE循环
1 使用while循环 current_number= : print(current_number) current_number += 2 让用户选择退出 prompt = "tell ...
- 今天练手了下mysqlbinlog,标记下
1 首先查看是否开启了 bin log 登录mysql后 使用命令 show variables like "log_%"; show binary logs; 2 确认开启了 ...
- Win10 C盘根目录权限
cmd管理员运行 icacls c:\ /setintegritylevel M c盘属性,安全,完全控制.