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. jquery fadeOut()方法 语法

    jquery fadeOut()方法 语法 作用:fadeOut() 方法使用淡出效果来隐藏被选元素,假如该元素是隐藏的.大理石平台精度等级 语法:$(selector).fadeOut(speed, ...

  2. curl: (35) Server aborted the SSL handshake 解决办法

    # 先删除curl brew uninstall curl # 重新安装curl,带上--with-openssl brew install curl --with-openssl # 然后重启下ph ...

  3. [c++] C++中public、protected、private的区别

    转:https://blog.csdn.net/vanturman/article/details/79393317 第一: private,public,protected的访问范围: privat ...

  4. Php+Redis函数使用总结

    因项目需求,冷落了redis,今天再重新熟悉一下: <?php //连接 $redis = New Redis(); $redis->connect('127.0.0.1','6379', ...

  5. Cortex-M3 在C中上报入栈的寄存器和各fault状态寄存器

    因为在标准C语音中是不能获取SP指针的.因而,如果想通过C代码来获取入栈的寄存器值,需要配合一小段汇编代码来获取当前的SP值,然后再把这个SP值以参数形式传送给C代码,最后以指针的形式把栈中的各寄存器 ...

  6. [java][转]安装ADT的时候,提示“Cannot complete the install because one or more required items could not be

    今天在安装ADT的时候,提示“Cannot complete the install because one or more required items could not be found.  S ...

  7. Python面向对象进阶和socket网络编程

    写在前面 为什么坚持?想一想当初: 一.面向对象进阶 - 1.反射补充 - 通过字符串去操作一个对象的属性,称之为反射: - 示例1: class Chinese: def __init__(self ...

  8. git备份代码

    仓库备份位置: /huawei-bak vim /huawei-bak/huawei-bak.sh #!/bin/bash#项目克隆下来后将其注释即可PROJECT="git@codehub ...

  9. spring boot 整合redis

    spring boot 中配置redis1 在pom.xml中增加相关包依赖:<dependency> <groupId>org.springframework.boot< ...

  10. php register_shutdown_function响应error 配合error_get_last 捕获错误

    转自: http://blog.csdn.net/a757291228/article/details/62231125 //一个函数,用来响应 register_shutdown_function ...