[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 ( ...
随机推荐
- mysql索引的操作
一.创建和查看普通索引 这是最基本的索引类型,而且它没有唯一性之类的限制 1.创建表时创建普通索引 CREATE TABLE table_name( 属性名 数据类型, ... 属性名 数据类型, I ...
- 安装cloudermanager时如何正确Configuring TLS Security for Cloudera Manager
不多说,直接上干货! 参考官网 https://www.cloudera.com/documentation/enterprise/5-2-x/topics/cm_sg_config_tls_secu ...
- Android彻底组件化demo发布
今年6月份开始,我开始负责对"得到app"的android代码进行组件化拆分,在动手之前我查阅了很多组件化或者模块化的文章,虽然有一些收获,但是很少有文章能够给出一个整体且有效的方 ...
- Hash二次探测
Hash的二次探测,当hash的长度为n:插入val,当Hash[val]不为0时,选择新地址newval = val +(-) 1*1,val+(-)2*2,val+(-)(n-1)*(n-1); ...
- html5——3D案例(立体导航)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- android studio 控件提示大写
方法一: 在第一行找到File进入找到setting,找到code completion 右侧复选框 选择-->None—->ok 方法二:<item name="andr ...
- (转) 基于Arcgis for Js的web GIS数据在线采集简介
http://blog.csdn.net/gisshixisheng/article/details/44310765 在前一篇博文“Arcgis for js之WKT和geometry转换”中实现了 ...
- (转) Arcgis for js之WKT和GEOMETRY的相互转换
http://blog.csdn.net/gisshixisheng/article/details/44057453 1.wkt简介 WKT(Well-known text)是一种文本标记语言,用于 ...
- codeforces_725C_字符串
C. Hidden Word time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- PAT_A1133#Splitting A Linked List
Source: PAT A1133 Splitting A Linked List (25 分) Description: Given a singly linked list, you are su ...