Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.

Return the intersection of these two interval lists.

(Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b.  The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval.  For example, the intersection of [1, 3] and [2, 4] is [2, 3].)

Example 1:

Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Reminder: The inputs and the desired output are lists of Interval objects, and not arrays or lists.
 class Solution {
public int[][] intervalIntersection(int[][] A, int[][] B) {
if (A == null || A.length == || B == null || B.length == ) {
return new int[][];
} int m = A.length, n = B.length;
int i = , j = ;
List<int[]> res = new ArrayList<>();
while (i < m && j < n) {
int[] a = A[i];
int[] b = B[j]; // find the overlap... if there is any...
int startMax = Math.max(a[], b[]);
int endMin = Math.min(a[], b[]); if (endMin >= startMax) {
res.add(new int[]{startMax, endMin});
} //update the pointer with smaller end value...
if (a[] == endMin) { i++; }
if (b[] == endMin) { j++; }
}
return res.toArray(new int[][]);
}
}

Interval List Intersections的更多相关文章

  1. [Swift]LeetCode986. 区间列表的交集 | Interval List Intersections

    Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order ...

  2. Leetcode 986. Interval List Intersections

    暴搜.. class Solution(object): def intervalIntersection(self, A: List[Interval], B: List[Interval]) -& ...

  3. LC 986. Interval List Intersections

    Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order ...

  4. 【leetcode】986. Interval List Intersections

    题目如下: Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted ...

  5. Java实现LeetCode #986 - Interval List Intersections

    class Solution { public: vector<Interval> intervalIntersection(vector<Interval>& A, ...

  6. 【leetcode】986. Interval List Intersections (双指针)

    You are given two lists of closed intervals, firstList and secondList, where firstList[i] = [starti, ...

  7. 【LeetCode】986. Interval List Intersections 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 题目地址:https://leetco ...

  8. Interval 间隔问题

    2018-09-07 09:03:14 一.Merge Intervals 问题描述: 问题求解: public List<Interval> merge(List<Interval ...

  9. [LeetCode] Merge Sorted Array 混合插入有序数组

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...

随机推荐

  1. laravel中遇到的坑

    已经遇到的坑和未来可能遇到的坑都将在这里写出来: 在资源控制器中创建新的方法后(如果资源控制器中的7个方法无法满足你的需求时,你就会创建新的方法),接下来就是创建路由,这个时候注意了,你必须要把路由放 ...

  2. cookbook 11.1 在文本控制台中显示进度条

    任务: 在进行长时间操作时,向用户显示一个"进度指示条". 解决方案: #coding=utf-8 import sys class progressbar(object): de ...

  3. Android蓝牙通信

    Android为蓝牙设备之间的通信封装好了一些调用接口,使得实现Android的蓝牙通信功能并不困难.可通过UUID使两个设备直接建立连接. 具体步骤: 1. 获取BluetoothAdapter实例 ...

  4. luoguP5024 保卫王国

    题目链接 问题分析 其实是比较明显的动态DP. 懒于再推一遍式子,直接用 最小权点覆盖=全集-最大权独立集,然后就和这道题一样了.题解可以看这里. 然后必须选或者不选的话,就直接把相应的点权变成\(- ...

  5. PTA 二叉树路径

    二叉树的路径 (25 分) 二叉树是一种普通的数据结构.给出一棵无限的二叉树,节点被标识为一对整数,构造如下:     (1)树根被标识为整数对(1,1).     (2)如果一个节点被标识为(a,b ...

  6. OUC_Summer Training_ DIV2_#5

    这是做的最好的一次了一共做了4道题  嘻嘻~ A - Game Outcome Time Limit:2000MS     Memory Limit:262144KB     64bit IO For ...

  7. Nginx-rtmp之配置项的管理

    1. 概述 Nginx-rtmp 对 rtmp{...} 内的配置项划分了几个级别: 直接隶属于 rtmp{} 块内的配置项称为 main 配置项. 直接隶属于 server{} 块内的配置项称为 s ...

  8. Qt新安装之后出现Error while building/deploying (kit: Desktop Qt 5.7.0 GCC 64bit) When executing step "Make”

      Ubuntu14.04初次安装Qt之后可能出现Error while building/deploying project *** (kit: Desktop Qt 5.7.0 GCC 64bit ...

  9. redis如何清空当前缓存和所有缓存

    Windows环境下使用命令行进行redis缓存清理1.redis安装目录下输入cmd2.redis-cli -p 端口号3.flushdb    清除当前数据库缓存4.flushall     清除 ...

  10. vue图片的处理技巧

    我们想用 post 向后台发送字符串类型的数据:我们可以不适用 data 来进行数据传输,而是用 params 来进行数据传输 代码的简洁之道:分模块化书写: vue 里面提供对图片的监听事件:loa ...