【LeetCode】57. Insert Interval
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]
.
由于insert和erase代价太大,需要移动后面所有元素。
所有空间换时间,返回新的数组ret,而不采用inplace做法。
主要以下三种情况:
1、newInterval与当前interval没有交集,则按照先后次序加入newInterval和当前interval,然后装入所有后续interval。返回ret。
2、newInterval与当前interval有交集,合并成为新的newInterval,然后处理后续interval。
3、处理完最后一个interval若仍未返回ret,说明newInterval为最后一个interval,装入ret。返回ret。
- /**
- * 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> ret;
- if(intervals.empty())
- {
- ret.push_back(newInterval);
- return ret;
- }
- int i = ;
- while(i < intervals.size())
- {
- //no overlapping
- if(newInterval.end < intervals[i].start)
- {
- ret.push_back(newInterval);
- while(i < intervals.size())
- {
- ret.push_back(intervals[i]);
- i ++;
- }
- return ret;
- }
- else if(newInterval.start > intervals[i].end)
- ret.push_back(intervals[i]);
- //overlapping
- else
- {
- newInterval.start = min(newInterval.start, intervals[i].start);
- newInterval.end = max(newInterval.end, intervals[i].end);
- }
- i ++;
- }
- ret.push_back(newInterval);
- return ret;
- }
- };
【LeetCode】57. Insert Interval的更多相关文章
- 【LeetCode】57. Insert Interval [Interval 系列]
LeetCode中,有很多关于一组interval的问题.大体可分为两类: 1.查看是否有区间重叠: 2.合并重叠区间; 3.插入新的区间: 4. 基于interval的其他问题 [ 做题通用的关键 ...
- 【一天一道LeetCode】#57. Insert Interval
一天一道LeetCode系列 (一)题目 Given a set of non-overlapping intervals, insert a new interval into the interv ...
- 【LeetCode】380. Insert Delete GetRandom O(1) 解题报告(Python)
[LeetCode]380. Insert Delete GetRandom O(1) 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxu ...
- 【leetcode】35-Search Insert Position
problem Search Insert Position 一种容易想到的是暴力破解法,一种是常用的二分法. 暴力破解法1(不推荐) class Solution { public: int sea ...
- [leetcode sort]57. Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- 【LEETCODE】57、数组分类,适中级别,题目:969、442、695
package y2019.Algorithm.array.medium; import java.util.ArrayList; import java.util.List; /** * @Proj ...
- 【LeetCode】701. Insert into a Binary Search Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】Search Insert Position(搜索插入位置)
这道题是LeetCode里的第35道题. 题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元 ...
- 【leetcode】1272. Remove Interval
题目如下: Given a sorted list of disjoint intervals, each interval intervals[i] = [a, b] represents the ...
随机推荐
- Spring与web MVC的整合——Spring的应用上下文管理
问题1 如何让web容器加载你的web MVC框架 对于基于servlet的web容器来说,遵循的是servlet规范,入口配置文件是web.xml.这类web容器会在启动的时候会而且仅会加载如下三种 ...
- mybatis映射文件遇到的小问题
mybatis的映射文件插入操作时: 如果对应的属性是String类型的,那么一定要做空串的判断. 比如注册的时候,如果需要向数据库中插入一条记录时,对应的字段没有给他赋值,这个String类型的值传 ...
- flink和spark stream等框架的对比
参考这篇文章: https://www.sohu.com/a/196257023_470008 我们当时的目标就是要设计一款低延迟.exactly once.流和批统一的,能够支撑足够大体量的复杂计算 ...
- 附 5 springboot之配置文件
本文转载自http://www.jianshu.com/p/80621291373b,作者:龙白一梦 我的boss 代码从开发到测试要经过各种环境,开发环境,测试环境,demo环境,线上环境,各种环境 ...
- C++ Explicit Constructors(显式构造函数)
C++ 为类(Class)提供了许多默认函数.如果自己没有申明,编译器会为我们提供一个copy构造函数.一个copy assignment操作符和一个析构函数.此外,如果没有申明任何构造函数,编译器会 ...
- RequireJS 参考文章
入门: http://www.cnblogs.com/snandy/archive/2012/05/22/2513652.html http://www.cnblogs.com/snandy/arch ...
- 如何在Windows 7 或Vista中修改MTU
Windows操作系统使用Maximum Transmission Unit (MTU) 来确定在下面的网络层上可以传输的协议数据包(protocol data packet)的最大尺寸. MTU参数 ...
- iOS开发-数据选择UIPickerView
UIPickerView开发一般选择区域或者分级数据的时候会使用到,类似于前端中用到树状结构,不过PC上一般都是从上到下的分级,使用UIPickView是从左到右实现,可以动态的设置UIPickVie ...
- Strategy 策略模式 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- DIV-CSS布局中position属性详解
本文向大家描述一下DIV CSS布局中的position属性的用法,position属性主要有四种属性值,任何元素的默认position的属性值均是static,静态.这节课主要讲讲relative( ...