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的更多相关文章

  1. 【LeetCode】57. Insert Interval [Interval 系列]

    LeetCode中,有很多关于一组interval的问题.大体可分为两类: 1.查看是否有区间重叠: 2.合并重叠区间;  3.插入新的区间: 4. 基于interval的其他问题 [ 做题通用的关键 ...

  2. 【一天一道LeetCode】#57. Insert Interval

    一天一道LeetCode系列 (一)题目 Given a set of non-overlapping intervals, insert a new interval into the interv ...

  3. 【LeetCode】380. Insert Delete GetRandom O(1) 解题报告(Python)

    [LeetCode]380. Insert Delete GetRandom O(1) 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxu ...

  4. 【leetcode】35-Search Insert Position

    problem Search Insert Position 一种容易想到的是暴力破解法,一种是常用的二分法. 暴力破解法1(不推荐) class Solution { public: int sea ...

  5. [leetcode sort]57. Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  6. 【LEETCODE】57、数组分类,适中级别,题目:969、442、695

    package y2019.Algorithm.array.medium; import java.util.ArrayList; import java.util.List; /** * @Proj ...

  7. 【LeetCode】701. Insert into a Binary Search Tree 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【LeetCode】Search Insert Position(搜索插入位置)

    这道题是LeetCode里的第35道题. 题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元 ...

  9. 【leetcode】1272. Remove Interval

    题目如下: Given a sorted list of disjoint intervals, each interval intervals[i] = [a, b] represents the ...

随机推荐

  1. easyui-datetimebox设置默认时分秒00:00:00

    datetimebox默认打开面板显示的是当前的时间,有个需求就是当打开面板时显示固定的”00:00:00”时间, 它本身有个方法spinner方法可以获得时间微调器对象,它所依赖的组件combo有个 ...

  2. PHP自己定义安装

    ① 自己定义安装(先要在管理里停止apache服务,再卸载apache.再安装时不须要重新启动电脑) apache+php+mysql+phpmyadmin自行安装 我们建议大家,安装的时候安装到同一 ...

  3. TCP三次握手四次挥手相关问题探讨

    TCP的握手挥手和状态转换是很多网络问题的基础.在此进行相关问题的讨论及记录. 首先,这幅图大致介绍了TCP连接和断开的过程: 注意其中的几个状态: LISTEN, SYN-SEND, SYN-RCV ...

  4. Windows Server上iSCSI的Best Practices

    Installing and Configuring Microsoft iSCSI Initiator http://technet.microsoft.com/en-us/library/ee33 ...

  5. API手册 常用功能

    directive [ng] a form input input [checkbox] input [email] input [number] input [radio] input [text] ...

  6. 运用CSS改进网站设计的3个技巧

    CSS是一种分离表述编码和实际内容的一种最有用的方式,也是一种使搜索引擎最优化的方式,通常,网站设计中会建立一整套的css样式标准,这些标准存储在一个扩展名为css的单独文件中.然后利用HTML标签设 ...

  7. IOS实现多媒体音频之音乐播放器

    随着智能手机市场越来越活跃,相应的app也变得五彩缤纷,各式各样,让你的app更吸引人多媒体技术不可避免.通过对音频和视频等控制让你的app更加丰富多彩,今天和大家一起研究下基本的音频使用.本文只提供 ...

  8. 应用程序在状态栏展示时间(C#)

    private DispatcherTimer _timer; private void SetTimeElaspInStatusBar() { try { _timer = new Dispatch ...

  9. Android网络缓存的实现思路

    在开发群里有多位同学问到了关于Android中网络缓存的问题.事实上不管是Android还是iOS,缓存的大致思路都是同样的,以下就几种情况下的缓存做一个大致的介绍.顺便说一下有些开源的网络请求框架已 ...

  10. js 继承概述

    上文讲述过js实现面向对象,一定是能够实现继承的效果的.尽管说非常多的js框架都帮助我们实现了继承的功能.或者说在日常的工作和学习中我们压根就用不到js的继承,可是我们还是须要了解一下js中继承.以方 ...