【LeetCode-面试算法经典-Java实现】【057-Insert Interval(插入区间)】
【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(插入区间)】的更多相关文章
- 057 Insert Interval 插入区间
给出一个无重叠的按照区间起始端点排序的区间列表.在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间).示例 1:给定区间 [1,3],[6,9],插入并合并 ...
- leetCode 57.Insert Interval (插入区间) 解题思路和方法
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ...
- LeetCode 57. Insert Interval 插入区间 (C++/Java)
题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...
- [LeetCode] Insert Interval 插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [LeetCode] 57. Insert Interval 插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [leetcode]57. Insert Interval插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- 【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】
[139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of w ...
- 【LeetCode-面试算法经典-Java实现】【053-Maximum Subarray(最大子数组和)】
[053-Maximum Subarray(最大子数组和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Find the contiguous subarray w ...
- 【LeetCode-面试算法经典-Java实现】【062-Unique Paths(唯一路径)】
[062-Unique Paths(唯一路径)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 A robot is located at the top-left c ...
随机推荐
- POJ 2299 Ultra-QuickSort【树状数组 ,逆序数】
题意:给出一组数,然后求它的逆序数 先把这组数离散化,大概就是编上号的意思--- 然后利用树状数组求出每个数前面有多少个数比它小,再通过这个数的位置,就可以求出前面有多少个数比它大了 这一篇讲得很详细 ...
- JS 前端 将图片转换为Base64利用H5 FileReader新特性
file = document.getElementById("image"); var file=file.files[0]; var fileName=file.name; ...
- iF.svnadmin 安装遇到的坑
iF.svnadmin 官网:http://svnadmin.insanefactory.com/ 安装配置iF.svnadmin : http://blog.linhere.com/archives ...
- sass的用法小结(二)
3. 导入SASS文件; css有一个特别不常用的特性,即@import规则,它允许在一个css文件中导入其他css文件.然而,后果是只有执行到@import时,浏览器才会去下载其他css文件,这导致 ...
- html5学习之第一步:认识标签,了解布局
图1. Acme United的网页的规划 Header区的例子包含了页面标题和副标题,< header>标签被用来创建页面的Header区的内容.除了网页本身之外,< header ...
- 关于数组array_diff(array1, array2)求差集来比较数组是否相等的问题细究
无意中发现很多朋友都喜欢使用array_diff(array1, array2)来判断两个数组是否相等, 我自己也偶尔会这么使用 但是今天我在写代码的过程中无意发现这么做是不准确的. 首先我们来看一下 ...
- ASP.NET-缓存outputcache参数
给Index加一个60秒的缓存,应该缓存在IIS服务器里面(我猜的) 只对变化的参数page不进行缓存,其他参数返回相同的内容 根据接受的语言的不同不进行缓存 设定缓存的位置 依赖于数据库变化的缓存 ...
- 使用Qt.labs.settings来存储应用的设置
我在曾经的文章中,讲述了怎样使用U1db及SQLite offline storage API来存储应用的一些状态.在这篇文章中,我将介绍怎样使用Qt.labs.settings来存储应用的状态.更加 ...
- MySQL 一台主机多实例root登录问题
假设在一台机子上起多个MySQL实例. 比方port号为 3306. 3307. 3308 登录时候要选择不同的 mysql.sock文件 mysql -uroot -p123456 这一句 登录的是 ...
- linux 抓包 tcpdump 简单应用
在linuxserver上,常常要定位网络问题,就须要用到抓包. 比如:tcpdump -X -s 0 host 10.17.81.22 and port 9999 -w /home/text.cap ...