[Swift Weekly Contest 122]LeetCode986. 区间列表的交集 | 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.

Note:

  1. 0 <= A.length < 1000
  2. 0 <= B.length < 1000
  3. 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,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
输出:[[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
注意:输入和所需的输出都是区间对象组成的列表,而不是数组或列表。

提示:

  1. 0 <= A.length < 1000
  2. 0 <= B.length < 1000
  3. 0 <= A[i].start, A[i].end, B[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 = 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 }

原文地址:https://www.cnblogs.com/strengthen/p/10351703.html

时间: 2024-10-31 16:17:03

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

[Swift Weekly Contest 122]LeetCode985. 查询后的偶数和 | Sum of Even Numbers After Queries

We have an array A of integers, and an array queries of queries. For the i-th query val = queries[i][0], index = queries[i][1], we add val to A[index].  Then, the answer to the i-th query is the sum of the even values of A. (Here, the given index = q

【python-leetcode42-区间合并】区间列表的交集

问题描述: 给定两个由一些闭区间组成的列表,每个区间列表都是成对不相交的,并且已经排序. 返回这两个区间列表的交集. (形式上,闭区间 [a, b](其中 a <= b)表示实数 x 的集合,而 a <= x <= b.两个闭区间的交集是一组实数,要么为空集,要么为闭区间.例如,[1, 3] 和 [2, 4] 的交集为 [2, 3].) 示例: 输入:A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]

[Swift Weekly Contest 108]LeetCode929. 独特的电子邮件地址 | Unique Email Addresses

Every email consists of a local name and a domain name, separated by the @ sign. For example, in [email protected], alice is the local name, and leetcode.com is the domain name. Besides lowercase letters, these emails may contain '.'s or '+'s. If you

[Swift Weekly Contest 117]LeetCode966.元音拼写检查 | Vowel Spellchecker

Given a wordlist, we want to implement a spellchecker that converts a query word into a correct word. For a given query word, the spell checker handles two categories of spelling mistakes: Capitalization: If the query matches a word in the wordlist (

[Swift Weekly Contest 118]LeetCode970. 强整数 | Powerful Integers

Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j for some integers i >= 0 and j >= 0. Return a list of all powerful integers that have value less than or equal to bound. You may return the answer in any ord

[Swift Weekly Contest 118]LeetCode971.翻转二叉树以匹配先序遍历 | Flip Binary Tree To Match Preorder Traversal

Given a binary tree with N nodes, each node has a different value from {1, ..., N}. A node in this binary tree can be flipped by swapping the left child and the right child of that node. Consider the sequence of N values reported by a preorder traver

[Swift Weekly Contest 118]LeetCode973. 最接近原点的 K 个点 | K Closest Points to Origin

We have a list of points on the plane.  Find the K closest points to the origin (0, 0). (Here, the distance between two points on a plane is the Euclidean distance.) You may return the answer in any order.  The answer is guaranteed to be unique (exce

[Swift Weekly Contest 128]LeetCode1013. 总持续时间可被 60 整除的歌曲 | Pairs of Songs With Total Durations Divisible by 60

In a list of songs, the i-th song has a duration of time[i] seconds. Return the number of pairs of songs for which their total duration in seconds is divisible by 60.  Formally, we want the number of indices i < j with (time[i] + time[j]) % 60 == 0.

[Swift Weekly Contest 108]LeetCode930. 和相同的二元子数组 | Binary Subarrays With Sum

In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] Note: A.length <= 30000 0 <= S <