Linux   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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 <

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:

Interval List Intersections

Input: A = [[0,2],[5,10],[13,23],[24,25]],B = [[1,5],[8,12],[15,24],[25,26]] Output: [[1,25]] Reminder: The inputs and the desired output are lists of Interval objects,and not arrays or lists.


 1 class@H_502_49@ Solution {
 2     public int[][] intervalIntersection(int[][] A,int@H_502_49@[][] B) {
 3         if (A == null || A.length == 0 || B == null || B.length == 0@H_502_49@) {
 4             return new int[0][0@H_502_49@];
 5 @H_502_49@        }
 6         
 7         int m = A.length,n =@H_502_49@ B.length;
 8         int i = 0,j = 0@H_502_49@;
 9         List<int[]> res = new ArrayList<>@H_502_49@();
10         while (i < m && j <@H_502_49@ n) {
11             int[] a =@H_502_49@ A[i];
12             int[] b =@H_502_49@ B[j];
13 
14             // find the overlap... if there is any...
15             int startMax = Math.max(a[0],b[0@H_502_49@]);
16             int endMin = Math.min(a[1],b[1@H_502_49@]);
17             
18             if (endMin >=@H_502_49@ startMaX) {
19                 res.add(new int@H_502_49@[]{startMax,endMin});
20 @H_502_49@            }
21             
22             //update the pointer with @R_696_11412@ller end value...
23             if (a[1] == endMin) { i++@H_502_49@; }
24             if (b[1] == endMin) { j++@H_502_49@; }
25 @H_502_49@        }
26         return res.toArray(new int[0][0@H_502_49@]);
27 @H_502_49@    }
28 }

大佬总结

以上是大佬教程为你收集整理的Interval List Intersections全部内容,希望文章能够帮你解决Interval List Intersections所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。