[Java]LeetCode57 Insert Interval
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的更多相关文章
- LeetCode57 Insert Interval
题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...
- [Swift]LeetCode57. 插入区间 | Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [OJ] Insert Interval
LintCode #30. Insert Interval (Easy) LeetCode #57. Insert Interval (Hard) class Solution { public: v ...
- 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 ...
- 【leetcode】Insert Interval
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- 60. Insert Interval && Merge Intervals
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- leetcode Insert Interval 区间插入
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Insert Interval 使用模拟 ...
- 【LeetCode】57. Insert Interval
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
随机推荐
- Python中的Map/Reduce
MapReduce是一种函数式编程模型,用于大规模数据集(大于1TB)的并行运算.概念"Map(映射)"和"Reduce(归约)",是它们的主要思想,都是从函数 ...
- MVC系列学习(二)-初步了解ORM框架-EF
1.新建 一个控制台项目 2.添加一个数据项 a.选择数据库 注:数据库中的表如下: b.选择EF版本 c.选择表 3.初步了解EF框架 看到了多了一个以 edmx后缀的文件 在edmx文件上,右击打 ...
- Scala-基础-变量与常量
import junit.framework.TestCase import org.junit.Test //变量 //var 代表变量 //val 代表常量 //关键字 class,extends ...
- Android开发高手课笔记 - 01 崩溃优化(上):关于“崩溃”那点事
Android 的两种崩溃 Java 崩溃就是在 Java 代码中,出现了未捕获的异常,导致程序异常退出 Native 崩溃一般都是因为在 Native 代码中访问非法地址,也可能是地址对齐出了问题, ...
- CSS——display:flex
Flex是Flexible Box的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性. 设为Flex布局以后,子元素的float.clear和vertical-align属性 ...
- 从ABC到流利口语-unit01
Unit 1 Introduction1 Good evening,everyone.It's a pleasure to you all. My name is Wang Dong.I'M from ...
- Mirror用法
switch (quadrantType) { case QuadrantType.one: db.setlayerCenter(); ids.Add(db.AddToModelSpace(arc)) ...
- -bash:whois:command not found
在centOS 下,如果出现-bash:whois:command not found的问题, 则yum install 安装whois软件 yum install -y jwhois 包名是jwho ...
- 安装 jdk 和 IDE软件
1.jdk的安装 通过官方网站获取JDK http://www.oracle.com 针对不同操作系统,下载不同的JDK版本 识别计算机的操作系统 下载完后进行安装,傻瓜式安装,下一步下一步即可.用j ...
- swift--如何设置子视图alpha不同于父视图
//1.2加入商家标题评分容器 let titleWarp=UIView(frame: CGRectMake(, , screenObject.width, )); titleWarp.backgro ...