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 ...
随机推荐
- 函数响应式编程(FRP)思想-Callback风格
序 ReactiveCocoa是IOS广为使用的技术框架,而ReactiveCocoa的核心思想就FRP.FRP不同于JAVA的object-oriented和AOP,FRP能让你的代码像数学一样简洁 ...
- BZOJ2179:FFT快速傅立叶(FFT)
Description 给出两个n位10进制整数x和y,你需要计算x*y. Input 第一行一个正整数n. 第二行描述一个位数为n的正整数x. 第三行描述一个位数为n的正整数y. Output 输出 ...
- 「Newcoder练习赛40D」小A与最大子段和
题目 挺好的一道题 我们考虑把\(i\)作为选取的最大子段的结束位置,我们如何往前计算贡献呢 考虑一下这个乘上其在队列中的位置可以表示为这个数被算了多少次,而我们往前扩展一位当前已经被扩展的就会被计算 ...
- js 实现分享功能
分享功能初步测试,title为当前页面的title. 其他详见注释!!! <!doctype html> <html> <head> <meta http-e ...
- “SAP.Middleware.Connector.RfcDestinationManager”的类型初始值设定项引发异常
在VS2015中使用SAP Connector 3.0(SapNco)的.net4.0x86版本开发时,程序运行到RfcDestinationManager.TryGetDestination时报错: ...
- Vue教程:指令与事件(二)
一.插值 v-once 通过使用 v-once 指令,你也能执行一次性地插值,当数据改变时,插值处的内容不会更新.但请留心这会影响到该节点上所有的数据绑定: span v-once>这个将不会改 ...
- css清除间隙
.clear{clear:both;height:0;width:0;line-height:0;overflow:hidden;}
- linux mysql命令行查看显示中文
linux 命令行查看mysql的库字符集是utf8,查询某个表时,仍然是显示不了中文, 之后使用了命令 mysql>set names utf8;就可以正常显示中文了. 如何才更好的使mys ...
- JSP Cookie 处理
Cookie是存储在客户机的文本文件,它们保存了大量轨迹信息.在servlet技术基础上,JSP显然能够提供对HTTP cookie的支持. 通常有三个步骤来识别回头客: 服务器脚本发送一系列cook ...
- iOS11.2-11.3.1进行越狱及问题
设备环境:Electra.iOS11.13.1 PS:Electra最新版本进行越狱只支持11.14以下的版本.同时这是不完美越狱,每次重启手机都需要重新越狱,最后,由于Electra版本推出仓促,一 ...