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. NSDate 刚刚、几分钟、几小时

    - (NSString *)minutesAgo { NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.dateFormat = @ ...

  2. .Net Mail SMTP 发送网络邮件

    刚刚迈入"开发"的行列 一直有一个想法 我什么时候能给我庞大的用户信息数据库给每一位用户邮箱发送推荐信息呢? 刚迈入"编程两个月的时间" 我采用 SMTP 发送 ...

  3. python之socket 网络编程

    提到网络通信不得不复习下osi七层模型: 七层模型,亦称OSI(Open System Interconnection)参考模型,是参考模型是国际标准化组织(ISO)制定的一个用于计算机或通信系统间互 ...

  4. 3. Builder(建造者)

    意图: 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 适用性: 当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时. 当构造过程必须允许被构造的对象有不同 ...

  5. .Net MVC+bootstrap Table学习

    一.效果展示 二.使用方法 1).相关css和js的引用 <link href="~/Themes/Bootstrap/css/bootstrap.css" rel=&quo ...

  6. VM安装OracleLinux

    http://blog.csdn.net/highning/article/details/11556077 一步一步教你在VMware Workstation 10 安装 Oracle Linux ...

  7. MMS源码中异步处理简析

    1,信息数据的查询,删除使用AsycnQueryHandler处理 AsycnQueryHandler继承了Handler public abstract class AsyncQueryHandle ...

  8. linux自动以root登录,并自动启动用户程序的设置方法

    系统自动以root登录,并自动启动用户程序的设置方法 第一步:删除root用户 vi /etc/passwd 该文件的第一行:root:X:0:0:root:/root:/bin/bash,只需要把第 ...

  9. SSMS错误:A connection was successfully established with the server, but then an error occurred during the login process

    参考: 系统太慢,实在搞不清是哪里的问题,祭出重装大法 需要安装的工具还真多,先装主要的吧.VS2013, SQL SERVER 2012,搞定.. 连个数据库试试,出错了: A connection ...

  10. DataTable 和Json 字符串互转

    #region DataTable 转换为Json字符串实例方法 /// <summary> /// GetClassTypeJosn 的摘要说明 /// </summary> ...