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. java中URL和File的相互转化

    首先,想比较一下这两者的不同.URL用于网络,所以带有明显的protocol,而且对于中文及符号支持的很不好.File就是我们平常系统中的文件路径了,对于中文及符号都支持,但是已经没有protocol ...

  2. Linux内核分析第九次作业

    理解进程调度时机跟踪分析进程调度与进程切换的过程 一.基础知识 Linux系统的一般执行过程 一般情况:正在运行的用户态进程X切换到运行用户态进程Y的过程 1. 正在运行的用户态进程X 2. 发生中断 ...

  3. webpack 中,module、chunk、bundle 的区别(待补充)

    项目 区别 module 是开发中的单个模块 chunk 中文意思是"块",是指 webpack 在进行模块依赖分析的时候,代码分割出来的代码块 bundle

  4. Struts2复习

    补充 声明处为什么要继承前面的actionsupport?继承action的execute等格式啊,更规范 声明处为什么后面实现一个model?数据封装到模型再返回去方便啊 值栈是什么啊?原理呐?存放 ...

  5. redis总结问题

    简单回顾了redis,在这过程中 首先得了解redis是什么,redis的运用场景,redis支持哪些数据格式,redis如何操作数据,redis如何实现高可用 redis是什么: Redis 是一个 ...

  6. vscode, cmake编译多个C++文件

    目的是利用vscode及相关插件编译多个C++文件. 我已经装好cmake和mingw并且将它们的路径添加到系统变量path中了. vscode装上如下几个插件: 点击vscode左上角   文件-& ...

  7. C++Primer第五版——习题答案详解(十)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第11章 关联容器 练习11.3 #include<iostream> #i ...

  8. .net 连接 Oracle 可能需要配置

    D:\Program Files (x86)\Oracle Developer Tools for VS2013\network\admin\tnsnames.ora

  9. 20165312 2017-2018-2《Java程序设计》第9周学习总结

    20165312 2017-2018-2<Java程序设计>第9周学习总结 上周错题总结 1.进程的基本状态有:新建.运行.阻塞.死亡. A . true B . false 解析:A 这 ...

  10. MySQL架构之 主从+ProxySQL实现读写分离

    准备服务器: docker run -d --privileged -v `pwd`/mysql_data:/data -p 3001:3306 --name mysql5-master --host ...