题目

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 newInterval同时还要保证能够merge。那么就分情况讨论。

遍历每一个已给出的interval,

当当前的interval的end小于newInterval的start时,说明新的区间在当前遍历到的区间的后面,并且没有重叠,所以res添加当前的interval;

当当前的interval的start大于newInterval的end时,说明新的区间比当前遍历到的区间要前面,并且也没有重叠,所以把newInterval添加到res里,并更新newInterval为当前的interval;

当当前的interval与newInterval有重叠时,merge interval并更新新的newInterval为merge后的。

代码如下:

 1     public ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) {
 2         ArrayList<Interval> res = new ArrayList<Interval>();
 3             
 4         for(Interval each: intervals){
 5             if(each.end < newInterval.start){
 6                 res.add(each);
 7             }else if(each.start > newInterval.end){
 8                 res.add(newInterval);
 9                 newInterval = each;        
             }else if(each.end >= newInterval.start || each.start <= newInterval.end){
                 int nstart = Math.min(each.start, newInterval.start);
                 int nend = Math.max(newInterval.end, each.end);
                 newInterval = new Interval(nstart, nend);
             }
         }
  
         res.add(newInterval); 
  
         return res;
     }

Reference://http://www.programcreek.com/2012/12/leetcode-insert-interval/

Insert Interval leetcode java的更多相关文章

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

  2. Merge Interval leetcode java

    题目: Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6] ...

  3. Search Insert Position leetcode java

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

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

  5. leetcode Insert Interval 区间插入

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Insert Interval 使用模拟 ...

  6. [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals

    Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...

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

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

  8. 【leetcode】Insert Interval

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

  9. 【LeetCode】57. Insert Interval

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

随机推荐

  1. 300万大奖:欢迎参加美团联合主办的全球AI挑战赛

    2018年8月29日,由美团.创新工场.搜狗.美图联合主办的“AI Challenger 2018全球AI挑战赛”正式启动.美团CTO罗道峰.创新工场CEO李开复.搜狗CEO王小川和美图CEO吴欣鸿共 ...

  2. Block的基本用法

    NSString* (^myBlock)(NSString*, int); myBlock = ^(NSString *name, int age){ return [NSString stringW ...

  3. ThinkPHP连接sqlserver,错误11001

    错误代码如下: :( 11001:[Microsoft][ODBC Driver 11 for SQL Server]TCP Provider: 不知道这样的主机. 0:[Microsoft][ODB ...

  4. Educational Codeforces Round 44 (Rated for Div. 2)

    题目链接:https://codeforces.com/contest/985 ’A.Chess Placing 题意:给了一维的一个棋盘,共有n(n必为偶数)个格子.棋盘上是黑白相间的.现在棋盘上有 ...

  5. 使用BasicDataSource引发的数据库连接中断的问题和解决方法

    http://blog.csdn.net/itbasketplayer/article/details/44198963 http://blog.sina.com.cn/s/blog_9e3e5499 ...

  6. Codeforces Round #359 (Div. 1) A. Robbers' watch 暴力

    A. Robbers' watch 题目连接: http://www.codeforces.com/contest/685/problem/A Description Robbers, who att ...

  7. bestcoder#23 1001 Sequence

    Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  8. JavaScript操作DOM(动态表格处理)

    <html> <title>动态处理表格数据</title> <head> <script type="text/javascript& ...

  9. mySql---or和in的效率问题(和<=、>=、between之间的关系)

    写在前面: 本文是直接拿取的别人的实验数据作参考,然后对数据作分析. 参考网友的测试数据结果: 在网上一直看到的是or和in的效率没啥区别,一直也感觉是这样,前几天刚好在看<mysql数据库开发 ...

  10. Vanish 详解

    1.varnish 概述:     varnish是一款高性能且开源的方向代理服务器和HTTP加速器,它的开发者poul-Henning kamp FreeBSD 核心的开发人员之一.varnish采 ...