LeetCode: 57. Insert Interval(Hard)
1. 原题链接
https://leetcode.com/problems/insert-interval/description/
2. 题目要求

该题与上一题的区别在于,插入一个新的interval对象,将此对象与所给列表里的Interval对象进行合并。
3. 解题思路
首先遍历Interval对象列表,找到插入位置。将插入位置之前的列表中的Interval对象直接加入结果列表。
然后查找所要插入Interval对象的end属性在列表中的位置。
最后将列表中剩余的Interval对象全部加入结果列表。
4. 代码实现
import java.util.LinkedList;
import java.util.List; public class InsertIntervals57 { public static void main(String[] args) { Interval in1 = new Interval(1, 2);
Interval in2 = new Interval(3, 5);
Interval in3 = new Interval(6, 7);
Interval in4 = new Interval(8, 10);
Interval in5 = new Interval(12, 16);
Interval in = new Interval(4, 9);
List<Interval> ls = new LinkedList<Interval>();
ls.add(in1);
ls.add(in2);
ls.add(in3);
ls.add(in4);
ls.add(in5);
for (Interval i : insert(ls, in))
System.out.println(i.start + " " + i.end); } public static List<Interval> insert(List<Interval> intervals, Interval interval) { List<Interval> res = new LinkedList<Interval>();
if (intervals.size() == 0) {
res.add(interval);
return res;
}
int i = 0; // 将插入位置之前的元素直接加入结果列表中
while (i < intervals.size() && intervals.get(i).end < interval.start) {
res.add(intervals.get(i++));
} // 将所要插入的元素与李彪中的对象进行合并
while (i < intervals.size() && intervals.get(i).start <= interval.end) {
interval = new Interval( // we could mutate newInterval here also
Math.min(interval.start, intervals.get(i).start),
Math.max(interval.end, intervals.get(i).end));
i++;
}
res.add(interval); // 将剩余的所有元素插入结果列表
while (i < intervals.size())
res.add(intervals.get(i++));
return res;
}
}
LeetCode: 57. Insert Interval(Hard)的更多相关文章
- 第一周 Leetcode  57. Insert Interval (HARD)
		
Insert interval 题意简述:给定若干个数轴上的闭区间,保证互不重合且有序,要求插入一个新的区间,并返回新的区间集合,保证有序且互不重合. 只想到了一个线性的解法,所有区间端点,只要被其 ...
 - 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 57.Insert Interval (插入区间) 解题思路和方法
		
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ...
 - [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 插入区间 (C++/Java)
		
题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...
 - Leetcode#57 Insert Interval
		
原题地址 遍历每个区间intervals[i]: 如果intervals[i]在newInterval的左边,且没有交集,把intervals[i]插入result 如果intervals[i]在ne ...
 - [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 57: Insert Interval 让代码更好读, 更容易测试.
		
阅读了几个博客, 决定写一个简易版本; 忙着做更多题, 没有时间多考虑优化代码, 所以, 就写一个试试运气. http://blog.csdn.net/kenden23/article/details ...
 
随机推荐
- Junit4所需jar包
			
在eclipse中新建一个Junit类,运行时出现java.lang.NoClassdeffounderror:org/apache/commons/logging/LogFactory错误,原来是缺 ...
 - Gluon sgd
			
from mxnet import nd,autograd,init,gluon from mxnet.gluon import data as gdata,loss as gloss,nn num_ ...
 - jQuery中$.ajax()详解(转)
			
JQuery中$.ajax()方法参数详解 url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. type: 要求为String类型的参数,请求方式(post或get) ...
 - Vue通过input筛选数据
			
<div id="app"> <input v-model='search' /> <ul> <li v-for="item i ...
 - 【转】计算Java List中的重复项出现次数
			
本文演示如何使用Collections.frequency和Map来计算重复项出现的次数.(Collections.frequency在JDK 1.5版本以后支持) package com.qiyad ...
 - 记一次重装系统后恢复EFS加密文件过程
			
之前用了8年的win7系统被我删注册表给折腾挂了, 无法进入系统, 无法进入安全模式, 无法使用光盘修复 只能重装系统,习惯性的重装前GHOST备份了一下 今天忽然发现有好几个项目文件居然成了绿色的, ...
 - Faster Alternatives to glReadPixels and glTexImage2D in OpenGL ES
			
In the development of Shou, I’ve been using GLSL with NEON to manipulate image rotation, scaling and ...
 - SecureCRT连接主机时(串口/网络),无法从键盘输入
			
Session Option-Connection-Serial-Flow Control,里面的选项全部取消掉,再重启CRT就ok了...
 - unittest单元测试框架之unittest 框架的总结2(八)
			
unittest 下的属性 1.Unittest.TestCase:所有测试用例类继承的基本类 2.Unittest.main():将一个单元测试模块变为可直接运行的测试脚本 If __name__ ...
 - Jstorm+Spring+mybatis整合
			
在现有的jstorm框架下,有一个需求:jstorm要对接mysql数据库的实时读取数据, 通过bolt处理,可能要调用service层的框架,最后保存到数据库. 在网上寻找了一下,发现storm集成 ...