LeetCode 986. [ python] Interval List Intersections
原题链接
中等
作者:
徐辰潇
,
2020-03-06 06:57:57
,
所有人可见
,
阅读 785
class Solution:
def intervalIntersection(self, A: List[List[int]], B: List[List[int]]) -> List[List[int]]:
res = []
i = 0
j = 0
while(i < len(A) and j < len(B)):
while(i < len(A) and A[i][1] < B[j][0]):
i+=1
if i == len(A):
break
while(j < len(B) and B[j][1] < A[i][0]):
j+=1
if j == len(B):
break
if A[i][1] < B[j][0] or A[i][0] > B[j][1]:
continue
interval = [0,0]
interval[0] = max(A[i][0], B[j][0])
interval[1] = min(A[i][1], B[j][1])
res.append(interval)
if A[i][1] < B[j][1]:
i+=1
else:
j+=1
return res