[Swift Weekly Contest 122]LeetCode986. 区间列表的交集 | Inte
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? 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:
给定两个由一些闭区间组成的列表,每个区间列表都是成对不相交的,并且已经排序。 返回这两个区间列表的交集。 (形式上,闭区间? 示例: 输入:A = [[0,B = [[1,26]] 输出:[[1,25]] 注意:输入和所需的输出都是区间对象组成的列表,而不是数组或列表。 提示:
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 = 0 17 for x in A 18 { 19 for y in B 20 { 21 var l:Int = max(x.start,y.start) 22 var r:Int = min(x.end,y.end) 23 if l <= r 24 { 25 C[p] = Interval(l,r) 26 p += 1 27 } 28 } 29 } 30 if (p - 1) < C.count 31 { 32 var arr = C[0...(p - 1)] 33 return [Interval](arr) 34 } 35 return C 36 } 37 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- xquery – MarkLogic的“xdmp:collection-delete”如何工作
- ASP.NET C#中Application的用法教程
- 如何使用其他属性和方法在C#中扩展DataRow和DataTable?
- .net – RadAjaxManager AJAX请求大约有10%的时间失败
- 13.4.1 读取值
- PostgreSQL 连接问题 FATAL: no pg_hba.conf entry for hos
- 正则表达式 – 使用sed一次排序2行
- php No 'Access-Control-Allow-Origin' header
- vuejs事件中心管理组件间的通信详解
- ORACLE 将一个库的部分值带条件插入到另外一个库