[Swift Weekly Contest 122]LeetCode986. 区间列表的交集 | Interval List Intersections

[Swift Weekly Contest 122]LeetCode986. 区间列表的交集 | Interval List Intersections,第1张

概述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:

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.

Note:

0 <= A.length < 1000 0 <= B.length < 1000 0 <= A[i].start,A[i].end,B[i].start,B[i].end < 10^9

给定两个由一些闭区间组成的列表,每个区间列表都是成对不相交的,并且已经排序。

返回这两个区间列表的交集

(形式上,闭区间 [a,b](其中 a <= b)表示实数 x 的集合,而 a <= x <= b。两个闭区间的交集是一组实数,要么为空集,要么为闭区间。例如,[1,3] 和 [2,4] 的交集为 [2,3]。)

示例:

输入:A = [[0,B = [[1,26]]输出:[[1,25]]注意:输入和所需的输出都是区间对象组成的列表,而不是数组或列表。

提示:

0 <= A.length < 1000 0 <= B.length < 1000 0 <= A[i].start,B[i].end < 10^9

1472 ms

 1 /** 2  * DeFinition for an interval. 3  * public class Interval { 4  *   public var start: Int 5  *   public var end: Int 6  *   public init(_ start: Int,_ end: Int) { 7  *     self.start = start 8  *     self.end = end 9  *   }10  * }11  */12 class Solution {13     func intervalintersection(_ A: [Interval],_ B: [Interval]) -> [Interval] {14         if A.isEmpty || B.isEmpty {return []}15         var C:[Interval] = [Interval](repeating:Interval(0,1),count:A.count*B.count)16         var p:Int = 017         for x in A18         {19             for y in B20             {21                 var l:Int = max(x.start,y.start)22                 var r:Int = min(x.end,y.end)23                 if l <= r24                 {25                     C[p] = Interval(l,r)26                     p += 127                 }28             }29         }30         if (p - 1) < C.count31         {32             var arr = C[0...(p - 1)]33             return [Interval](arr)34         }35         return C36     }37 }
总结

以上是内存溢出为你收集整理的[Swift Weekly Contest 122]LeetCode986. 区间列表的交集 | Interval List Intersections全部内容,希望文章能够帮你解决[Swift Weekly Contest 122]LeetCode986. 区间列表的交集 | Interval List Intersections所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1020005.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存