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

乍看起来不难的一题,但实现的时候还是需要很清晰的逻辑才能用简洁代码完成。

把遍历时的情况列举一下,无非就三种:

1. 当前区间的end小于给定区间的start, 则continue;

2. 当前区间的start大于给定区间的end, 说明给定区间正好在它前面,直接把给定区间插入在当前区间前并停止遍历。

3. 当前区间与给定区间有重叠。

主要是第三种情况比较复杂,细分的话还能分成好几种情况。实际上不需要考虑这么细致,如果只关心最后插入的区间,那么就不用过多考虑中间遍历到的有重叠的区间,只需要根据它们的start和end来调整最后插入的给定区间范围,然后删去它们即可。

具体的调整方法为: 给定区间的start为当前区间start与给定区间start的小的那个值。end为较大的那个值。

注意,如果遍历完了(没有在遍历中插入给定区间), 那么给定区间一定在最后,直接插到数组最后即可。

     public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
int start = newInterval.start;
int end = newInterval.end;
List<Interval> re = new ArrayList<Interval>(intervals);
for(int i=0;i<re.size();i++) {
Interval temp = re.get(i);
if(end<temp.start) {
re.add(i, new Interval(start, end));
return re;
}
if(temp.end<start)
continue;
else {
start = Math.min(temp.start, start);
end = Math.max(temp.end, end);
re.remove(i);
i--;
}
}
re.add(new Interval(start, end));
return re;
}

[Leetcode][JAVA] Insert Interval的更多相关文章

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

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

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

  3. 【leetcode】Insert Interval

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

  4. Leetcode: Merge/Insert Interval

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

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

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

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

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

  7. Java for LeetCode 057 Insert Interval

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

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

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

  9. LeetCode: 57. Insert Interval(Hard)

    1. 原题链接 https://leetcode.com/problems/insert-interval/description/ 2. 题目要求 该题与上一题的区别在于,插入一个新的interva ...

随机推荐

  1. 简单的html和css

    整体图太大了,看不太清楚,下面是分开的图 第一张: 第二张:

  2. Windows自带.NET Framework版本大全

    The following is a complete list of which version of the .NET Framework is included in which version ...

  3. ui-router中的锚点问题(angular中的锚点问题)

    angular.module('anchorScrollExample', []) .controller('ScrollController', ['$scope', '$location', '$ ...

  4. SQL 查询性能优化----解决书签查找

    先来看看什么是书签查找: 当优化器所选择的非聚簇索引只包含查询请求的一部分字段时,就需要一个查找(lookup)来检索其他字段来满足请求.对一个有聚簇索引的表来说是一个键查找(key lookup), ...

  5. JavaScript的继承

    原型继承的实现 1 简化版本 function SuperClass(){...} function SubClass(){...} SubClass.prototype=new SuperClass ...

  6. JS Encode and Decode URL

    1.Encode URL String var url = $(location).attr('href'); //get current url //OR var url = 'folder/ind ...

  7. Linux下SVN客户端安装及使用

    转载自:http://www.linuxidc.com/Linux/2015-01/111748.htm 不想自己写了,这个写的挺全的,我就按这个步骤走的,呵呵 非常感谢作者 环境说明: 系统版本:C ...

  8. [ubuntu] adb devices出现no permissions

    简书排版 http://www.jianshu.com/p/46e8848c6646 今天把一款测试的华为手机带回家,发现无法联机调试 笔者操作系统是 ubuntu 14.04 如果是windows找 ...

  9. flask-admin众博客概述

    最近用flask admin(https://flask-admin.readthedocs.org/en/latest/)构建自动化发布平台,发现flask admin蛮强大的,基本上不需要自己写太 ...

  10. 说说C#的async和await 解决卡顿问题 转

    C# 5.0中引入了async 和 await.这两个关键字可以让你更方便的写出异步代码. 看个例子: 可以看到,async和await关键字只是把上面的代码变得更简单易懂而已. public cla ...