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 ...
随机推荐
- FASIC: A Fast-recovery, Adaptively Spanning In-band Control Plane in Software-Defined Network
2017 IEEE Global Communications Conference 问题:in-band网络中如果物理链路阻塞或者硬件故障,导致控制器的消息不能及时到达各个交换机导致网络不一致甚至某 ...
- 在 Ubuntu16.04 中搭建 Spark 单机开发环境 (JDK + Scala + Spark)
1.准备 本文主要讲述如何在Ubuntu 16.04 中搭建 Spark 2.11 单机开发环境,主要分为 3 部分:JDK 安装,Scala 安装和 Spark 安装. JDK 1.8:jdk-8u ...
- [Cyan之旅]使用NPOI实现Excel的导入导出,踩坑若干.
Cyan是博主[Soar360]自2014年以来开始编写整理的工具组件,用于解决现实工作中常用且与业务逻辑无关的问题. 什么是NPOI? NPOI 是 POI 项目的 .NET 版本.POI是一个开源 ...
- java 多维数据定义
//一维数组定义与输出class less02{ public static void main(String[] args) { int stu[]=new int[]{1,2 ...
- sublime text3 增加emmett插件
本内容基于Windows环境)一.已安装有Sublime Text3 二.安装Package Control 安装这个后,可以在线安装所需的插件 方法1.Ctrl+~打开控制台,在控制台输 ...
- C# 多线程初级汇总
异步委托 创建线程的一种简单方式是定义一个委托,并异步调用它 委托是方法的类型安全的引用 Delegate类还支持异步地调用方法.在后台,Delegate类会创建一个执行任务的线程 投票,并检查委托是 ...
- SpringMVC处理ajax请求的跨域问题和注意事项
.首先要知道ajax请求的核心是JavaScrip对象和XmlHttpRequest,而浏览器请求的核心是浏览器我的个人博客(基于SSM,Redis,Tomcat集群的后台架构) github:htt ...
- Tour HDU - 3488(最大权值匹配)
Tour In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 30000) one- ...
- C++时间标准库时间time
转自:http://www.cnblogs.com/yukaizhao/archive/2011/04/29/cpp_time_system_time.html (玉开) C++标准库中的时间需要引用 ...
- svn问题汇总
1 svn图标 2 问题 SVN删除文件 一.本地删除SVN删除文件中的本地删除,指的是在客户端delete了一个文件,但还没有commit,使用revert来撤销删除. 二.服务器删除1.通过本地删 ...