【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 ...
随机推荐
- 关于exsi的虚拟网络
相关术语: VM Network 默认的网络 VSwitch (默认)标准交换机 (exsi的内核中) Port groups (用来定义各个不同vlan) DVSwitch分布式交换机(exs ...
- swift中高阶函数map、flatMap、filter、reduce
Swift相比于Objective-C又一个重要的优点,它对函数式编程提供了很好的支持,Swift提供了map.filter.reduce这三个高阶函数作为对容器的支持. 1 map:可以对数组中的每 ...
- POJ 3261 Milk Patterns(后缀数组+单调队列)
题意 找出出现k次的可重叠的最长子串的长度 题解 用后缀数组. 然后求出heigth数组. 跑单调队列就行了.找出每k个数中最小的数的最大值.就是个滑动窗口啊 (不知道为什么有人写二分,其实写啥都差不 ...
- Ubuntu16.04 lnmp 环境搭建
Ubuntu16.04 lnmp 环境搭建 nginx 安装 sudo apt-add-repository ppa:nginx/stablesudo apt-add-repository ppa:o ...
- vue中的methods、computed和watch
1.computed属性: 经过处理返回的数据值,只要源数据没有发生改变,computed函数里面对相应的数据就不会反生改变,相当于缓存在本地;发生改变的时候,computed对应数据的函数才会发生改 ...
- (51) magento集成增加到款通知
这篇主要讲述如何二开增加自己的功能,我没有继承方式二开,习惯是不好的,直接改了原来的模块. 达到效果就这样,当在网站支付成功,会同步到ERP系统中.下面来讲述实现过程 建立文件 payment_not ...
- pandas 8 画图
from __future__ import print_function import pandas as pd import numpy as np import matplotlib.pyplo ...
- 题解 洛谷 P1580 【yyy loves Easter_Egg I】
一言不合上代码: #include<cstdio> #include<cstring> ],bz[],dmz[]; int maohao,xf,ls,sss,lll,xxf,x ...
- (原创)VS2017 C# 运行 Javasrcipt RSA 加密用户名登录 Java开发的服务器
第一次写博客. 最近想做一个Web的自动登录,用户名和密码是RSA加密过的,后台是用的JAVA,我只会点C#,抓包什么都搞定了(使用的是Fiddler),不过由于C#和RSA的加密方式不同,我搞了N天 ...
- Redit集群搭建-Sentinel模式搭建
Redit集群搭建 学习了: Windows:http://blog.csdn.net/mrxiagc/article/details/52799081 Linux:https://www.cnblo ...