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:

Input: intervals = [[1,3],[6,9]], newInterval = [2,5]
Output: [[1,5],[6,9]]

题目

给定一堆区间,插入一个新区间。

思路

代码

 class Solution {
     public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
          List<Interval> result = new ArrayList<Interval>();
         for (Interval i : intervals) {
             // non-overlapping      | i |--|newInterval|
             if (newInterval == null || i.end < newInterval.start){
                 result.add(i);
             }
             //non-overlapping   |newInterval|--| i |
             else if (i.start > newInterval.end) {
                 result.add(newInterval);
                 result.add(i);
                 newInterval = null;// guarantee not entring if (newInterval != null)
             }
             // overlapping, update start and end
             else {
                 newInterval.start = Math.min(newInterval.start, i.start);
                 newInterval.end = Math.max(newInterval.end, i.end);
             }
         }
         if (newInterval != null)
             result.add(newInterval);
         return result;
     }
 }

[leetcode]57. Insert Interval插入区间的更多相关文章

  1. LeetCode 57. Insert Interval 插入区间 (C++/Java)

    题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...

  2. leetCode 57.Insert Interval (插入区间) 解题思路和方法

    Insert Interval  Given a set of non-overlapping intervals, insert a new interval into the intervals ...

  3. [LeetCode] 57. Insert Interval 插入区间

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

  4. [LeetCode] 57. Insert Interval 解决思路

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

  5. 第一周 Leetcode 57. Insert Interval (HARD)

    Insert interval  题意简述:给定若干个数轴上的闭区间,保证互不重合且有序,要求插入一个新的区间,并返回新的区间集合,保证有序且互不重合. 只想到了一个线性的解法,所有区间端点,只要被其 ...

  6. 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 ...

  7. [LeetCode] Insert Interval 插入区间

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

  8. 057 Insert Interval 插入区间

    给出一个无重叠的按照区间起始端点排序的区间列表.在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间).示例 1:给定区间 [1,3],[6,9],插入并合并 ...

  9. Leetcode#57 Insert Interval

    原题地址 遍历每个区间intervals[i]: 如果intervals[i]在newInterval的左边,且没有交集,把intervals[i]插入result 如果intervals[i]在ne ...

随机推荐

  1. 什么是pytorch(3神经网络)(翻译)

    神经网络 torch.nn 包可以用来构建神经网络. 前面介绍了 autograd包, nn 依赖于 autograd 用于定义和求导模型. nn.Module 包括layers(神经网络层), 以及 ...

  2. Linux touch命令详解

    Linux touch命令 Linux touch命令用于修改文件或者目录的时间属性,包括存取时间和更改时间.若文件不存在,系统会建立一个新的文件. 用法: touch [-acfm][-d<日 ...

  3. Windows IIS安装php

    [可参考文档:https://docs.microsoft.com/en-us/iis/application-frameworks/scenario-build-a-php-website-on-i ...

  4. 搜狗浏览器总是打开123.sogou.com-记搜狗浏览器遭遇劫持一例

    昨日,因从网上下载了office2010破解工具,压缩包中有个文件为名为[office 2010激活工具\为保证永久激活,要先点击这个配置,再点击KMSELDIYI激活.exe],单击之后没有反应.后 ...

  5. Python Pycharm 专题

    http://www.themesmap.com/theme.html?t=time&page=3 一些好的主题地址 直接导入import settings就可以使用了

  6. Mongodb 批量Upsert

    List<UpdateOneModel<Entity>> requests = new List<UpdateOneModel<Entity>>(ent ...

  7. webservice的简单使用,cxf框架的的使用

    Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配置这些应用程序,用于开发分布 ...

  8. feign接口调用异常的解决方向

    1. consul: 检查调用方服务与被调用方服务是否在同一个consul; 2. swagger: 检查swagger注释是否清晰.恰当: 比如: @ApiImplicitParams(value ...

  9. python中定义的颜色

    平时学习工作中,我们经常会接触到一些大佬写的Python实用工具,运行起来总会显示出五颜六色的背景,相关的定义在matplotlib模块中,为方便使用,这里给大家展示一下在这个模块中都定义了哪些选颜色 ...

  10. Java并发编程75个问答

    1.在java中守护线程和本地线程区别? java中的线程分为两种:守护线程(Daemon)和用户线程(User). 任何线程都可以设置为守护线程和用户线程,通过方法Thread.setDaemon( ...