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 ...
随机推荐
- redis持久化那些事(kēng)儿
这是一篇包含了介绍性质和吐槽性质的日志.主要介绍一下我学习redis持久化时候被坑的经历.redis的使用介绍现在没有打算写,因为比较多,以我如此懒的性格...好吧,还是有点这方面想法的,不过一篇博客 ...
- SpringMVC错误小结
No mapping found for HTTP request with URI [/SpringMVC/user.do] in DispatcherServlet with name 'spri ...
- Architecture pattern & Architecture style
Architecture pattern: context + problem -> solution Architecture style: solution part of architec ...
- 【转】Impala安装json解析udf插件
背景 Impala跟Hive一样,是常用的数据仓库组件之一.熟悉Hive的同学肯定知道,Hive官方提供了get_json_object函数用于处理json字符串,但是Impala官方并没有提供类似的 ...
- idea删除module
先remove , 再点击delete
- Redis(RedisTemplate)使用list链表
RedisTemplate配置:https://www.cnblogs.com/weibanggang/p/10188682.html package com.wbg.springRedis.test ...
- vue路由页面加载的几种方法~
懒加载 (1)定义:懒加载也叫延迟加载,即在需要的时候进行加载,随用随载. (2)为什么需要懒加载: 在单页应用中,如果没有应用懒加载,运用webpack打包后的文件将会异常的大,造成进入首页时,需要 ...
- 【oracle笔记3】多表查询
*多表查询 分类:1.合并结果集 2.连接查询 3.子查询 *合并结果集:要求被合并的表中,列的类型和列数相同. *UNION,去除重复行.完全相同的行会被去除 *UNION ALL:不去除重复行. ...
- Swift_类型选择
Swift_类型选择 点击查看源码 //类型选择 func test() { class MediaItem { } class Movie: MediaItem { } class Song: Me ...
- c++11线程创建的三种方法
一.用一个初始函数创建一个线程 直接看代码:注意c++在运行一个可执行程序的时候(创建了一个进程),会自动的创建一个主线程,这个主线程和进程同生共死,主线程结束,进程也就结束了. #include & ...