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. ETL工具-KETTLE教程专栏1----术语和定义

    1-资源库 资源库是用来保存转换任务的,用户通过图形界面创建的的转换任务可以保存在资源库中.        资源库可以使多用户共享转换任务,转换任务在资源库中是以文件夹形式分组管理的,用户可以自定义文 ...

  2. Codeforces 785 E. Anton and Permutation(分块,树状数组)

    Codeforces 785 E. Anton and Permutation 题目大意:给出n,q.n代表有一个元素从1到n的数组(对应索引1~n),q表示有q个查询.每次查询给出两个数l,r,要求 ...

  3. HDU 5810 Balls and Boxes ——(数学,概率,方差)

    官方题解看不太懂,参考了一些人的博客以后自己证明如下: 其中D(X)和E(X)的公式如下(参考自百度百科): 其中 p = 1 / m .(这是每一个单独事件发生的概率期望,在这里单独事件指的是一个球 ...

  4. Log4j rootLogger根配置以及4种日志级别

    Log4j 根配置语法 log4j.rootLogger = [ level ] , appenderName, appenderName, … 把指定级别以上的日志信息输出到指定的一个或者多个位置 ...

  5. Vue avoid mutating a prop directly since the value will be overwritten

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. 解决json_encode中文乱码问题

    关键字JSON_UNESCAPED_UNICODE即Json不要编码Unicode. $arr={"name":"你好"}; json_encode($arr, ...

  7. 利用Viewpager和Fragment实现UI框架的搭建实现

    package com.loaderman.uiframedemo; import android.os.Bundle; import android.support.v4.app.Fragment; ...

  8. Mysql强制修改密码

    windows: 1,停止MYSQL服务,CMD打开DOS窗口,输入 net stop mysql 2,在CMD命令行窗口,进入MYSQL安装目录 比如E:\Program Files\MySQL\M ...

  9. python 学习笔记(一):在列表、字典、集合中根据条件筛选数据

    一.在列表中筛选数据 在列表中筛选出大于等于零的数据,一般通用的用法代码如下: data = [3, -9, 0, 1, -6, 3, -2, 8, -6] #要筛选的原始数据列表 result = ...

  10. Ansible变量嵌套解析

    有时候需要用到ansible的变量的嵌套解析,就是“变量中嵌套变量”.例子如下: 假设有一个外部传递的变量,system_code = CRM,而我们同时有一系列类似的变量,如: ABS_port=1 ...