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. expect使用demo

    #!/usr/bin/expect set timeout set ip [lindex $argv ] spawn ssh root@$ip expect { "yes/no" ...

  2. funny_python 01 import antigravity

    在Python里面import antigravity 会发现另一个小彩蛋 Tada~ 其实就是打开了xkcd的一个python主题漫画 鼠标停留在图片上,还会看到标题: "I wrote ...

  3. Java Sha1 加密算法

    //下面四个import放在类名前面 包名后面 //import java.io.UnsupportedEncodingException; //import java.security.Messag ...

  4. String类的构造方法详解

    package StringDemo; //String类的构造方法详解 //方法一:String(); //方法二:String(byte[] bytes) //方法三:String (byte[] ...

  5. 解决: maven编译项目报“非法字符: \65279 ”错误

    打包maven项目的时候,出现异常: [INFO] ------------------------------------------------------------------------ [ ...

  6. 'autocomplete="off"'在Chrome 中不起作用

    大家都知道autocomplete属性是表单字段中的HTML5新属性,该属性有两种状态值,分别为"on" 和 "off",该属性可省略:省略属性值后默认值为&q ...

  7. 网络-->监控-->OID-->BGP

    说明:暂时发现只适合cisco设备,h3c的交换机只支持部分OID(支持版本.AS号.ROUTER-ID)

  8. nodejs npm常用命令

    npm是一个node包管理和分发工具,已经成为了非官方的发布node模块(包)的标准.有了npm,可以很快的找到特定服务要使用的包,进行下载.安装以及管理已经安装的包. 1.npm install m ...

  9. Codeforces 703B (模拟) Mishka and trip

    题目:这里 题意:n个城市,每个城市有个魅力值vi,首先,有n条路将这n个城市连成一个环,1号城市连2号城市,2号连3号****n号连1号城市,每条路的魅力值是其连接的两个城市 的魅力值的乘积,这n个 ...

  10. Jquery div展开收缩

    <html> <meta http-equiv="Content-Type" content="text/html; charset=gb2312&qu ...