Insert Interval & Merge Intervals
Insert Intervals
Given a non-overlapping interval list which is sorted by start point.
Insert a new interval into it, make sure the list is still in order and non-overlapping (merge intervals if necessary).
Insert [2, 5] into [[1,2], [5,9]], we get [[1,9]].
Insert [3, 4] into [[1,2], [5,9]], we get [[1,2], [3,4], [5,9]].
分析:
Create a new array list, insert the smaller interval in the new array and use a counter to keep track of the total number of smaller intervals. If we find an interval overlapping with the new one, we need to change the start and end.
class Solution {
public int[][] insert(int[][] intervals, int[] newInterval) {
List<int[]> resultList = new ArrayList<>();
boolean hasInserted = false;
for (int[] interval : intervals) {
if (interval[] > newInterval[]) {
if (!hasInserted) {
resultList.add(newInterval);
hasInserted = true;
}
resultList.add(interval);
} else if (interval[] < newInterval[]) {
resultList.add(interval);
} else {
newInterval[] = Math.min(newInterval[], interval[]);
newInterval[] = Math.max(newInterval[], interval[]);
}
}
if (!hasInserted) {
resultList.add(newInterval);
}
return resultList.toArray(new int[][]);
}
}
Merge Intervals
Given a collection of intervals, merge all overlapping intervals.
Given intervals => merged intervals:
[ [
[1, 3], [1, 6],
[2, 6], => [8, 10],
[8, 10], [15, 18]
[15, 18] ]
]
Analyze:
Sort first, then merge intervals if they overlap.
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public List<Interval> merge(List<Interval> list) {
if (list == null || list.size() <= ) return list; // Collections.sort(list, (Interval a, Interval b) -> a.start - b.start);
list.sort((i1, i2) -> Integer.compare(i1.start, i2.start)); for (int i = ; i < list.size(); i++) {
if (overlap(list.get(i), list.get(i - ))) {
list.get(i - ).start = Math.min(list.get(i).start, list.get(i - ).start);
list.get(i - ).end = Math.max(list.get(i).end, list.get(i - ).end);
list.remove(i);
i--;
}
}
return list;
} boolean overlap(Interval i1, Interval i2) {
return Math.max(i1.start, i2.start) <= Math.min(i1.end, i2.end);
}
}
Insert Interval & Merge Intervals的更多相关文章
- 60. Insert Interval && Merge Intervals
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- 56. Merge Intervals 57. Insert Interval *HARD*
1. Merge Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[ ...
- 合并区间 · Merge Intervals & 插入区间 · Insert Interval
[抄题]: 给出若干闭合区间,合并所有重叠的部分. 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8, 10] ...
- [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals
Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...
- leetcode 56. Merge Intervals 、57. Insert Interval
56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...
- Leetcode: Merge/Insert Interval
题目 Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[ ...
- 间隔问题,合并间隔(merge interval),插入间隔(insert interval)
Merge Interval: Given a collection of intervals, merge all overlapping intervals. For example,Given ...
- 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 ...
随机推荐
- Python网络数据采集二
一.正则表达式 * 表匹配0次或者多次 a*b* + 表至少一次 [ ] 匹配任意一个 ( ) 辨识一个编组 {m,n} m或者n 次 [^] 匹配任意不在中括号里的字符 | ...
- 软件工程之四则运算--Github
由于现在配置问题,然后借用同学电脑将代码上传至Github,网址为:https://github.com/be821/RealFour 参考相关Github文档: 1. http://my.oschi ...
- 第二次spring冲刺第2天
今天我们开了个小会,关于讨论开始页面的设计及数据输入的格式限制.运算功能等改善
- [福大软工] Z班 团队作业——UML设计 作业成绩
团队作业--UML设计 作业链接 http://www.cnblogs.com/easteast/p/7745703.html 作业要求 1)团队分工(5分) 描述团队的每个成员分别完成了UML图的哪 ...
- VSTS写单元测试
用VSTS写单元测试 许多应用程序都会用到“用户”类型,今天我要用的是ConsoleApplicatio ...
- Spring之AOP注解配置
1.导入相应jar包 2.引入约束并配置XML文件 <beans xmlns="http://www.springframework.org/schema/beans" xm ...
- WP-PostViews使用
1.在后台安装次插件 2.获取多少天之内的访问排名最高的记录 2.1 添加相应方法代码到wp-postviews.php文件中,据体代码可以网上找(本人自己可以在自己本机的例子查看到),这里只是记录大 ...
- JDK8新特性,给接口添加一个默认实现
在JDK8中,允许给接口本身添加一个默认的实现.用“default”进行修饰.如下实例 package interfacetest; public interface TestInterface { ...
- Mobile Phone Network CodeForces - 1023F(并查集lca+修改环)
题意: 就是有几个点,你掌控了几条路,你的商业对手也掌控了几条路,然后你想让游客都把你的所有路都走完,那么你就有钱了,但你又想挣的钱最多,真是的过分..哈哈 游客肯定要对比一下你的对手的路 看看那个便 ...
- Spring Shell介绍
最近开发中在下遇到了spring-shell开发工具的项目,现在整理了相关文章,以供大家学习 本博客相关的文章均是在Spring Shell 1.2.0的基础上建立 Spring Shell介绍 ...