[Leetcode][JAVA] 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]
.
乍看起来不难的一题,但实现的时候还是需要很清晰的逻辑才能用简洁代码完成。
把遍历时的情况列举一下,无非就三种:
1. 当前区间的end小于给定区间的start, 则continue;
2. 当前区间的start大于给定区间的end, 说明给定区间正好在它前面,直接把给定区间插入在当前区间前并停止遍历。
3. 当前区间与给定区间有重叠。
主要是第三种情况比较复杂,细分的话还能分成好几种情况。实际上不需要考虑这么细致,如果只关心最后插入的区间,那么就不用过多考虑中间遍历到的有重叠的区间,只需要根据它们的start和end来调整最后插入的给定区间范围,然后删去它们即可。
具体的调整方法为: 给定区间的start为当前区间start与给定区间start的小的那个值。end为较大的那个值。
注意,如果遍历完了(没有在遍历中插入给定区间), 那么给定区间一定在最后,直接插到数组最后即可。
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
int start = newInterval.start;
int end = newInterval.end;
List<Interval> re = new ArrayList<Interval>(intervals);
for(int i=0;i<re.size();i++) {
Interval temp = re.get(i);
if(end<temp.start) {
re.add(i, new Interval(start, end));
return re;
}
if(temp.end<start)
continue;
else {
start = Math.min(temp.start, start);
end = Math.max(temp.end, end);
re.remove(i);
i--;
}
}
re.add(new Interval(start, end));
return re;
}
[Leetcode][JAVA] Insert Interval的更多相关文章
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- 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 ( ...
- Leetcode: Merge/Insert Interval
题目 Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[ ...
- leetCode 57.Insert Interval (插入区间) 解题思路和方法
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ...
- 第一周 Leetcode 57. Insert Interval (HARD)
Insert interval 题意简述:给定若干个数轴上的闭区间,保证互不重合且有序,要求插入一个新的区间,并返回新的区间集合,保证有序且互不重合. 只想到了一个线性的解法,所有区间端点,只要被其 ...
- Java for LeetCode 057 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(Hard)
1. 原题链接 https://leetcode.com/problems/insert-interval/description/ 2. 题目要求 该题与上一题的区别在于,插入一个新的interva ...
随机推荐
- funny_python 01 import antigravity
在Python里面import antigravity 会发现另一个小彩蛋 Tada~ 其实就是打开了xkcd的一个python主题漫画 鼠标停留在图片上,还会看到标题: "I wrote ...
- 【Android端 APP 启动时长获取】启动时长获取方案及具体实施
一.什么是启动时长? 1.启动时长一般包括三种场景,分别是:新装包的首次启动时长,冷启动时长.热启动时长 冷启动 和 热启动 : (1)冷启动:当启动应用时,后台没有该程序的进程,此时启动的话系统会分 ...
- oracle 表被锁定 杀死进程
/*查出被锁biao*/ select b.owner,b.object_name,a.session_id,a.locked_mode from v$locked_object a,dba_obje ...
- PostrgreSQL 表名大小些问题(public."tablename")
问题: 今天做表查询的时候,发现用以前的代码查询出现问题,提示说表名不存在. 现象: 通过PostrgreSQL客户端查询,发现出问题的表的查询语句如下: SELECT * FROM public.& ...
- python打印服务器所有进程
#有时候我们需要查看服务器上所有进程,来判断哪些进程是否已经称为僵尸进程#!/usr/local/bin/python3.5 import psutil for i in psutil.pids(): ...
- tunning-Instruments and Flame Graphs
On mac os, programs may need Instruments to tuning, and when you face too many probe messages, you'l ...
- selenium2(WebDriver)环境搭建
1.安装jdk并配置环境变量: jdk安装jdk下载地址: http://www.oracle.com/technetwork/java/javase/downloads/index.html环境变量 ...
- WCF账户密码认证
记录一下我实现WCF用户认证与权限控制的实现方法, 也让其他网友少走一些弯路. 内容写得非常小白(因为我也是小白嘛), 比较详细, 方便WCF知识基础薄的朋友 主要分为下面几个步骤 作为例子, 创建最 ...
- SQL Server 列存储性能调优(翻译)
原文地址:http://social.technet.microsoft.com/wiki/contents/articles/4995.sql-server-columnstore-performa ...
- Java实现中文数字转换为阿拉伯数字
/** * 中文數字转阿拉伯数组[十万九千零六十 --> 109060] * @author 雪见烟寒 * @param chineseNumber * @return */ @Suppress ...