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

题意:该题与56题非常相似,只是题意给出每一个区间的start是递增的。所以我们不须要排序。该题须要我们加入一个区间。然后进行融合。

这题在56题的基础上添加了区间推断的复杂度。

包括例如以下如所看到的的六种情况。



而这六种情况又能够合并成三种解决方案。看例如以下代码:

/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
// 先推断newInterval是否在intervals的范围内
if (newInterval == null)
return intervals;
int len = intervals.size();
if (len == 0)
{
intervals.add(newInterval);
return intervals;
}
List<Interval> res=new ArrayList<Interval>();
for(Interval interval:intervals)
{
if(interval.end<newInterval.start)//newInterval在中间的情况
{
res.add(interval);
}else if(interval.start>newInterval.end)//newInterval插入最前端的情况
{
res.add(newInterval);
newInterval=interval;//这个地方非常重要。就是找到了待插入区间位置。指定新的newInterval,由于intervals中的区间也可能有相交的地方,须要融合。
}else if(interval.start<=newInterval.end||interval.end>=newInterval.start)//有重合部分的四种情况
{
newInterval=new Interval(Math.min(interval.start,newInterval.start),Math.max(interval.end,newInterval.end));
}
}
res.add(newInterval);
return res;
}
}

[Java]LeetCode57 Insert Interval的更多相关文章

  1. LeetCode57 Insert Interval

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

  2. [Swift]LeetCode57. 插入区间 | Insert Interval

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

  3. [OJ] Insert Interval

    LintCode #30. Insert Interval (Easy) LeetCode #57. Insert Interval (Hard) class Solution { public: v ...

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

  5. 【leetcode】Insert Interval

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

  6. 60. Insert Interval && Merge Intervals

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

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

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

  8. leetcode Insert Interval 区间插入

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

  9. 【LeetCode】57. Insert Interval

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

随机推荐

  1. Linq学习(零)-错误汇总

    问题一: Cannot execute text selection: CS0009 Metadata file 'C:\Users\Kimisme\Documents\LINQPad Plugins ...

  2. [Android]异常1-duplicate files during packaging of

    异常原因: 可能一>项目存在重复资源文件 可能二>Android Studio与源代码Android Studio不一致 解决方法有: 解决一>查找重复资源,删除或者修改文件 解决二 ...

  3. Hash二次探测

    Hash的二次探测,当hash的长度为n:插入val,当Hash[val]不为0时,选择新地址newval = val +(-) 1*1,val+(-)2*2,val+(-)(n-1)*(n-1); ...

  4. 我的 Windows 10 的基本配置

    Windows 10 的基本配置 功能性 开启 .Net Framework 3.5(包括 .NET 2.0 和 3.0) 旧版本 Windows 10 默认只安装了 .Net Framework 4 ...

  5. java网络

    title: java 网络 date: 2017年3月11日11:14:52 1. 复杂的东西就把他封装成对象 概述:(网络就是找到别人) 找到对方的机器,(找到对方的ip地址) 每个机器中有很多进 ...

  6. (转)postgis常用函数介绍(二)

    http://blog.csdn.net/gisshixisheng/article/details/47903151 概述: 书接上文,本文继续讲解Postgres中常用的空间函数的使用. 常用函数 ...

  7. 戴尔14G服务器用H740P配置阵列

    公司采购了几台dell r740机器.做阵列的方式跟之前ctrl+r有很大改动. 戴尔14G机器已经面世一段时间了,14G的机器使用过后都能发现器性能比上一代机器提升了很多,今天给大家带来戴尔14代服 ...

  8. 数字化婚姻配对尝试问题(C++实现)

    问题描述:一.标题: 数字化婚姻配对尝试 二.题目: 建立一个模型,来模拟推导社会男女择偶过程. 为了模型简化,一个人的特性指标有三个,这里假设为财富.样貌.品格,每个指标均可取值1-100之间任意数 ...

  9. mitmproxy安装与使用

    mitmproxy安装与使用 (抓包,中间人代理工具.支持SSL) 在开发微信公端的时候开发调试只能用浏览器自带开发工具,本来移动端可以用用fiddler.wireshark等工具来抓包,但是自从改用 ...

  10. is_NaN的使用

    原生js中使用判断某个值是否是数值,有且只有一个方法就是is_NaN. 原理:这个函数使用了Number() 去转换需要判断的值.Number() 去转换值,如果有任意非数值字符存在则就不是一个数值. ...