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. BZOJ 1778: [Usaco2010 Hol]Dotp 驱逐猪猡 概率与期望+高斯消元

    这个还挺友好的,自己相对轻松能想出来~令 $f[i]$ 表示起点到点 $i$ 的期望次数,则 $ans[i]=f[i]\times \frac{p}{q}$ #include <cmath> ...

  2. BZOJ 3782: 上学路 Lucas+ExCRT+容斥+dp

    其实呢,扩展中国剩余定理还有一种理解方式:就是你有一坨东西,形如:$A[i]\equiv B[i](mod$ $P[i])$. 对于这个东西,你可以这么思考:如果最后能求出一个解,那么这个解的增量一定 ...

  3. Confluence 6.15 附件宏参数

    参数 参数名称 默认值 描述 Filename Patterns(patterns) all   Attachment Labels(labels) (None) 标签(labels)的列表,用来过滤 ...

  4. 微信小程序“反编译”

    https://www.jianshu.com/p/ad8f417219e9 https://segmentfault.com/a/1190000018592740?utm_source=tag-ne ...

  5. 免费馅饼~-~ (hdu 1176

    当我准备要写这个随笔的时候是需要勇气的. 掉馅饼嘛,肯定是坑. (hdu1176 话说,gameboy人品太好,放学回家路上有馅饼可捡.还就在0~10这11个位置里,当馅饼开始掉的时候,gameboy ...

  6. How to correctly set application badge value in iOS 8?

    o modify the badge under ios8 you have to ask for permissions let settings = UIUserNotificationSetti ...

  7. Linux Ubuntu 用c++11编译

    加上: -std=c++ 例如: g++ test.

  8. SRS之SrsServer::cycle()

    1. SrsServer 相关类定义 1.1 SrsServer 类定义 /** * SRS RTMP server, initialize and listen, * start connectio ...

  9. Springboot websocket使用

    1)基本概念 1.http与websocket http超文本传输协议,大家都非常熟悉,http有1.0.1.1. 2.0几个版本,从http1.1起,默认都开启了Keep-Alive,保持连接持续性 ...

  10. vue中如何刷新页面

    vue中刷新页面的方法 1. 不能使用 this.$router.go(0) 或者 window.reload() 不起作用还特别恶心 这个才是有效果的刷新页面,只要照图敲,就能有效果 我们在 app ...