【057-Insert Interval(插入区间)】


【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】

原题

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

题目大意

  给定一系列非覆盖的区间,插入一个新的区间。有必要的时候进行区间合并。区间開始是以起始时间进行合并的

解题思路

  假设原来的区间比插入区间小就插入结果集,假设插入区间有重叠,更新插入区间。假设插入区间小于原来的区间,先插入插入区间。再增加大的区间

代码实现

算法实现类

import java.util.LinkedList;
import java.util.List; public class Solution { public List<Interval> insert(List<Interval> intervals, Interval newInterval) { // 保存结果的集合
List<Interval> result = new LinkedList<>(); // 输入集非空
if (intervals != null) {
// 遍历元素
for (Interval item : intervals) {
// newInterval == null 表示插入的区间已经处理完了
// 将比插入区间小的区间增加结果集中
if (newInterval == null || item.end < newInterval.start) {
result.add(item);
}
// 将比插入区间大的区间增加结果集中,同一时候将插入的区间增加结果集
else if (item.start > newInterval.end) {
result.add(newInterval);
result.add(item);
newInterval = null;
}
// 插入区间有重叠,更新插入区间
else {
newInterval.start = Math.min(newInterval.start, item.start);
newInterval.end = Math.max(newInterval.end, item.end);
}
}
} // 假设插入区间非空说明插入区间还未被处理
if (newInterval != null) {
result.add(newInterval);
} return result;
}
}

评測结果

  点击图片,鼠标不释放。拖动一段位置。释放后在新的窗体中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47164431

【LeetCode-面试算法经典-Java实现】【057-Insert Interval(插入区间)】的更多相关文章

  1. 057 Insert Interval 插入区间

    给出一个无重叠的按照区间起始端点排序的区间列表.在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间).示例 1:给定区间 [1,3],[6,9],插入并合并 ...

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

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

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

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

  4. [LeetCode] Insert Interval 插入区间

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

  5. [LeetCode] 57. Insert Interval 插入区间

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

  6. [leetcode]57. Insert Interval插入区间

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

  7. 【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

    [139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of w ...

  8. 【LeetCode-面试算法经典-Java实现】【053-Maximum Subarray(最大子数组和)】

    [053-Maximum Subarray(最大子数组和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Find the contiguous subarray w ...

  9. 【LeetCode-面试算法经典-Java实现】【062-Unique Paths(唯一路径)】

    [062-Unique Paths(唯一路径)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 A robot is located at the top-left c ...

随机推荐

  1. c语言中,常见数据类型的字节数

    一直记不住这个,特意mark下来 和机器字长及编译器有关系: 所以,int,long int,short int的宽度都可能随编译器而异.但有几条铁定的原则(ANSI/ISO制订的):  1 size ...

  2. SpringBoot学习笔记(11)-----SpringBoot中使用rabbitmq,activemq消息队列和rest服务的调用

    1. activemq 首先引入依赖 pom.xml文件 <dependency> <groupId>org.springframework.boot</groupId& ...

  3. 3dmax快速实现一个逼真地毯效果

    3dsmax怎么制作逼真的毛绒地毯模型?3dsmax中想要中想要建模长方形的毛绒地毯,该怎么制作呢?下面我们就来看看详细的教程,需要的朋友可以参考下: 1.在创建面板-扩展基本体下选择切角长方体 2. ...

  4. [NOIP补坑计划]NOIP2014 题解&做题心得

    六道普及组题,没啥好说的 场上预计得分:100+100+100+100+100+100=600(省一分数线490) (AK是不可能AK的,这辈子不可能AK的) 题解: D1T1 生活大爆炸版石头剪刀布 ...

  5. spring慕课网

    资源链接 http://spring.io/ http://projects.spring.io/spring-framework/ Spring是什么? Spring是一个开源的轻量级的应用开发框架 ...

  6. Cause of 400 Bad Request Errors

    The 400 Bad Request error displays inside the Internet browser window, just as web pages do. Cause o ...

  7. 重载和const形参

    1.int lookup(string p); 2.int lookup(const string p);//同1 3.int lookup(string *);//传入一个指针,指针指向string ...

  8. virtualbox挂载目录失败mount: unknown filesystem type ‘vboxsf’

    转自[https://blog.csdn.net/u011486871/article/details/79443375] [有小修改] 错误提示:Vagrant was unable to moun ...

  9. MapReduce中combine、partition、shuffle的作用是什么

    http://www.aboutyun.com/thread-8927-1-1.html Mapreduce在hadoop中是一个比較难以的概念.以下须要用心看,然后自己就能总结出来了. 概括: co ...

  10. POJ 2442 Sequence(堆的使用练习)

    题目地址:id=2442">POJ 2442 真心没想到这题的思路. .原来是从第一行逐步向下加,每次都仅仅保存前n小的数.顺便练习了下堆.. 只是感觉堆的这样的使用方法用的不太多啊. ...