给定两个由一些 闭区间 组成的列表,每个区间列表都是成对不相交的,并且已经排序。
返回这两个区间列表的交集。
(形式上,闭区间 [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]]
提示:
0 <= A.length < 1000
0 <= B.length < 1000
0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/interval-list-intersections
class Solution:
def intervalIntersection(self, A: List[List[int]], B: List[List[int]]) -> List[List[int]]:
A_len, B_len = len(A), len(B)
ans = []
for i in range(A_len):
for j in range(B_len):
if A[i][1] < B[j][0] or B[j][1] < A[i][0]:
continue
one = A[i][0] if A[i][0] >= B[j][0] else B[j][0]
tow = A[i][1] if A[i][1] <= B[j][1] else B[j][1]
ans.append(list((one,tow)))
return ans