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. PythonStudy——字典的定义 Dictionary definition

    # 空字典 d1 = {} d2 = dict() # 用map映射创建字典 d3 = dict({'a': 1, 'b': 1}) print(d3) # 用关键字赋值方式 d4 = dict(na ...

  2. Devexpress Winform MVVM

    归纳总结备忘 Devexpress Winform MVVM Practice 前言 MVVM Devexpress 正文 databindings及 UI Triggers Command 委托Co ...

  3. docker之 网络模式和跨主机通信

    Docker的四种网络模式Bridge模式 当Docker进程启动时,会在主机上创建一个名为docker0... Docker的四种网络模式 Bridge模式 当Docker进程启动时,会在主机上创建 ...

  4. Java面试题 Web+EJB & Spring+数据结构& 算法&计算机基础

    六.Web 部分:(共题:基础40 道,基础37 道,中等难度3 道) 122.说出Servlet 的生命周期,并说出Servlet 和CGI 的区别? [基础] 答:Web 容器加载Servlet ...

  5. 6.ST LINK 下调试异常

    ☆1.无法进入main函数(printf的影响)***为什么有时候可以进入main函数,有什么进入不了main函数?    <1> 因为C语言默认使用显示器作为标准输出的设备,所以如果想利 ...

  6. 3.CM3内核架构-寄存器

    一.寄存器的种类

  7. denyhosts、中文文档乱码、端口占用查询

    1.安装 denyhosts, 设置 hosts.allow ,系统自动将攻击的ip 添加如 hosts.deny2.打开中文文档乱码, 将文档下载到windows, 通过富文本编辑器查看文档编码3. ...

  8. thinkphp5 与 endroid 二维码生成

    windows compser安装endroid/qrcode,自己安装好composer工具; 1. 项目目录 文件 composer.json require 里添加 "endroid/ ...

  9. python中定义的颜色

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

  10. Calendar打印日历

    package com.example.demo; import org.junit.Test; import org.junit.runner.RunWith; import org.springf ...