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).

Example

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.

Example

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的更多相关文章

  1. 60. Insert Interval && Merge Intervals

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  2. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  3. 56. Merge Intervals 57. Insert Interval *HARD*

    1. Merge Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[ ...

  4. 合并区间 · Merge Intervals & 插入区间 · Insert Interval

    [抄题]: 给出若干闭合区间,合并所有重叠的部分. 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8, 10] ...

  5. [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals

    Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...

  6. leetcode 56. Merge Intervals 、57. Insert Interval

    56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...

  7. Leetcode: Merge/Insert Interval

    题目 Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[ ...

  8. 间隔问题,合并间隔(merge interval),插入间隔(insert interval)

    Merge Interval: Given a collection of intervals, merge all overlapping intervals. For example,Given  ...

  9. 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 ...

随机推荐

  1. Jquery封装ajax

    Jquery封装ajax   Load方法     <!-- 将jquery.js导入进来 -->     <script type="text/javascript&qu ...

  2. 如何用Qt自动拷贝exe依赖的dll

    QT生成的.exe文件不能运行的解决办法 之前的数独项目的GUI,当我的Qt项目生成exe时,由于缺少了相关的依赖dll文件,打开会一直报缺少依赖文件的错: 然后一开始我到安装的Qt文件夹里把这些有Q ...

  3. PAT 甲级 1086 Tree Traversals Again

    https://pintia.cn/problem-sets/994805342720868352/problems/994805380754817024 An inorder binary tree ...

  4. php排序学习之-冒泡排序

    原理:对一组数据,比较相邻数据的大小,将值小数据在前面,值大的数据放在后面.   (以下都是升序排列,即从小到大排列) 举例说明: $arr = array(6, 3, 8, 2, 9, 1); $a ...

  5. Windows 下 Docker 的简单学习使用过程之一 dockertoolbox

    1. Windows 下面运行 Docker 的两个主要工具1): Docker for Windows2): DockerToolbox区别:Docker For Windows 可以理解为是新一代 ...

  6. msyql: navicat 连接时msyql遇到的问题

    1.使用 mysql的用户,密码连接 服务器上的MySQL时,连接不上,报 100xx的错误. 1)原因是,MySQL默认情况下,只允许本地连接[127.0.0.1,或localhost]来连接mys ...

  7. angular浏览器滚动条滚动到指定element 触发事件

    angular.module('app').directive('ScrollTrigger', () => { return { restrict: "A", link:f ...

  8. Kivy 中文教程 实例入门 简易画板 (Simple Paint App):2. 实现绘图功能

    1. 理解 kivy 坐标系统 上一节中,咪博士带大家实现了画板程序的基础框架,以及一个基本的自定义窗口部件(widget).在上一节的末尾,咪博士留了一道关于 kivy 坐标系统的思考题给大家.通过 ...

  9. LOJ#551 Matrix

    本地打表在线AC什么的最喜欢了. 题意 \(\rm Alice\)和\(\rm Bob\)在玩游戏,他们要给一个\(n\times n\)的矩阵打标记.初始时没有任何标记,每一轮\(\rm Bob\) ...

  10. 安装及调试 Mavem Web

    一  使用Mavem eclipse菜单栏,找到file-->new -->other 然后找到Maven Project 然后next. 接着,选择maven-archetype-web ...