Interval List Intersections
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的更多相关文章
- [Swift]LeetCode986. 区间列表的交集 | Interval List Intersections
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order ...
- Leetcode 986. Interval List Intersections
暴搜.. class Solution(object): def intervalIntersection(self, A: List[Interval], B: List[Interval]) -& ...
- LC 986. Interval List Intersections
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order ...
- 【leetcode】986. Interval List Intersections
题目如下: Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted ...
- Java实现LeetCode #986 - Interval List Intersections
class Solution { public: vector<Interval> intervalIntersection(vector<Interval>& A, ...
- 【leetcode】986. Interval List Intersections (双指针)
You are given two lists of closed intervals, firstList and secondList, where firstList[i] = [starti, ...
- 【LeetCode】986. Interval List Intersections 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 题目地址:https://leetco ...
- Interval 间隔问题
2018-09-07 09:03:14 一.Merge Intervals 问题描述: 问题求解: public List<Interval> merge(List<Interval ...
- [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 ...
随机推荐
- BZOJ2208连通数
还是挺简单的tarjan. 判断时可能重复,直接bitset搞定. 首先tarjan缩点,每个scc的内部肯定能互相到达,更一下,而且一个scc里的各个点的贡献肯定是一样的,topsort,更新答案就 ...
- Java——重写hashCode()和euqals()方法
1.顺序表的问题 查找和去重效率较低 对于这样的顺序表来说,如果需要查找元素,就需要从第一个元素逐个检查,进行查找.对于需要去重的存储来说,每次存入一个元素之前,就得将列表中的每个元素都比对一遍,效率 ...
- k8s简单介绍
k8s是什么? 它是用来解决容器部署,调度,伸缩等基础的功能的软件 k8s的优点? 易学:轻量级,简单,容易理解 便携:支持公有云,私有云,混合云,以及多种云平台 可拓展:模块化,可插拔,支持钩子,可 ...
- 《视觉SLAM十四讲》第1讲
目录 一 视觉SLAM 注:原创不易,转载请务必注明原作者和出处,感谢支持! 一 视觉SLAM 什么是视觉SLAM? SLAM是Simultaneous Localization and Mappin ...
- Jeecg页面标签规则
1.表单字段重复校验方法 <input name="departname" class="inputxt" value="${depart.de ...
- javascript之DOM(Document Object Model) 文档对象模型
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- 详解nohup和& 区别
nohup 一.[解释] 不挂断地运行命令.no hangup的缩写,意即“不挂断”.一般理解&记住一个命令最简单的方法是记住它是什么缩写,就自然理解了这个命令.nohup运行由 Comman ...
- 1、puppet基础
Puppet:IT基础设施自动化管理工具 参考文章: https://yq.aliyun.com/articles/120228 http://www.51niux.com/?id=105 http: ...
- django实现利用mailgun进行收发邮件
django窗口类运用和邮件收发 运用django窗口类来完成表单html 1 具体你看网址: https://www.cnblogs.com/guguobao/p/9322027.html 利用窗口 ...
- 掌握Pod-Pod调度策略
一 Pod生命周期管理 1.1 Pod生命周期 Pod在整个生命周期过程中被系统定义了如下各种状态. 状态值 描述 Pending API Server已经创建该Pod,且Pod内还有一个或多个容器的 ...