Java for LeetCode 057 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]
.
解题思路一:
参考Java for LeetCode 056 Merge Intervals思路一,去掉最外层循环即可,JAVA实现如下:
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
if (newInterval == null)
return intervals;
int startIndex = 0, endIndex = 0;
for (int j = 0; j < intervals.size(); j++) {
if (newInterval.start > intervals.get(j).end) {
startIndex += 2;
endIndex += 2;
continue;
}
if (newInterval.end < intervals.get(j).start)
break;
if (newInterval.start >= intervals.get(j).start)
startIndex++;
if (newInterval.end > intervals.get(j).end) {
endIndex += 2;
continue;
}
if (newInterval.end >= intervals.get(j).start)
endIndex++;
break;
}
if(startIndex==endIndex&&startIndex%2==0)
intervals.add(startIndex/2,new Interval(newInterval.start,newInterval.end));
else if(startIndex%2==0&&endIndex%2==0){
intervals.get(startIndex/2).start=newInterval.start;
intervals.get(startIndex/2).end=newInterval.end;
for(int k=1;k<endIndex/2-startIndex/2;k++)
intervals.remove(startIndex/2+1);
}
else if(startIndex%2==0&&endIndex%2!=0){
intervals.get(startIndex/2).start=newInterval.start;
intervals.get(startIndex/2).end=intervals.get(endIndex/2).end;
for(int k=1;k<=endIndex/2-startIndex/2;k++)
intervals.remove(startIndex/2+1);
}
else if(startIndex%2!=0&&endIndex%2==0){
intervals.get(startIndex/2).end=newInterval.end;
for(int k=1;k<endIndex/2-startIndex/2;k++)
intervals.remove(startIndex/2+1);
}
else if(startIndex%2!=0&&endIndex%2!=0){
intervals.get(startIndex/2).end=intervals.get(endIndex/2).end;
for(int k=1;k<=endIndex/2-startIndex/2;k++)
intervals.remove(startIndex/2+1);
}
return intervals;
}
解题思路二:
参考Java for LeetCode 056 Merge Intervals思路二,添加后重新排序即可,JAVA实现如下:
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
if (intervals == null)
return intervals;
List<Interval> list = new ArrayList<Interval>();
intervals.add(newInterval);
Comparator<Interval> comparator = new Comparator<Interval>() {
@Override
public int compare(Interval o1, Interval o2) {
if (o1.start == o2.start)
return o1.end - o2.end;
return o1.start - o2.start;
}
};
Collections.sort(intervals, comparator);
for (Interval interval : intervals) {
if (list.size() == 0 || list.get(list.size() - 1).end < interval.start)
list.add(new Interval(interval.start, interval.end));
else
list.get(list.size() - 1).end = Math.max(interval.end, list.get(list.size() - 1).end);
}
return list;
}
Java for LeetCode 057 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 题意简述:给定若干个数轴上的闭区间,保证互不重合且有序,要求插入一个新的区间,并返回新的区间集合,保证有序且互不重合. 只想到了一个线性的解法,所有区间端点,只要被其 ...
- 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 ...
- 057 Insert Interval 插入区间
给出一个无重叠的按照区间起始端点排序的区间列表.在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间).示例 1:给定区间 [1,3],[6,9],插入并合并 ...
随机推荐
- 【ZOJ 1221】Risk
题 题意 给你20个城市的相邻关系,求给定任意两个城市的最短距离 分析 求任意两个城市最短距离,就是用floyd算法,我脑残忘记了k是写在最外层的. 代码 #include<stdio.h> ...
- BZOJ3720 Gty的妹子树
Description 我曾在弦歌之中听过你, 檀板声碎,半出折子戏. 舞榭歌台被风吹去, 岁月深处尚有余音一缕…… Gty神(xian)犇(chong)从来不缺妹子…… 他来到了一棵妹子树下,发现每 ...
- TCP/IP详解 学习二
链路层: 在 T C P / I P协议族中,链路层主要有三个目的:(1)为 I P模块发送和接收 I P数据报:( 2)为 A R P模块发送 A R P请求和接收 A R P应答:( 3)为 R ...
- Hibernate 缓存机制
一.why(为什么要用Hibernate缓存?) Hibernate是一个持久层框架,经常访问物理数据库. 为了降低应用程序对物理数据源访问的频次,从而提高应用程序的运行性能. 缓存内的数据是对物理数 ...
- IbatisNet的介绍和使用
http://dragonsuc.cnblogs.com/archive/2006/07/04/ibatisnet.html IbatisNet的介绍和使用http://files.cnblogs.c ...
- Android模拟器端口被占用问题的解决办法
一.问题描述 今天在Eclipse中运行Android项目时遇到"The connection to adb is down, and a severe error has occured& ...
- MyEclipse------如何连接MySQL
testconnection.jsp <%@ page language="java" import="java.util.*" pageEncoding ...
- NetBeans快捷键的使用
.Ctrl-Tab:在打开的文件之间切换: .Ctrl-N:在当前打开的项目里新建文件: .Ctrl-F:当前文件查找匹配的字符(支持正则): .Ctrl-H:当前文件查找.替换匹配的字符(支持正则, ...
- try、catch 和 throw 语句 (了解)
C++ 异常使用 try.catch 和 throw 关键字. 引发表达式指示错误或异常情况. 可以将任何类型的对象用作引发表达式的操作数. 此对象通常用于传达有关错误的信息. 通常,应使用在标准库中 ...
- Visual Studio Online Integrations
...