[Med 2ptr]986. Interval List Int
2019-10-24 本文已影响0人
Mree111
Description
给定两个interval序列,输出所有intersection interval
Solution
T O(N)
class Solution:
def intervalIntersection(self, A: List[List[int]], B: List[List[int]]) -> List[List[int]]:
startA = 0
startB = 0
interval = []
while startA < len(A) and startB < len(B):
if A[startA][1] < B[startB][0]:
startA+=1
elif A[startA][0] > B[startB][1]:
startB +=1
else:
interval.append([max(A[startA][0],B[startB][0]),min(A[startA][1],B[startB][1])])
if A[startA][1]>B[startB][1]:
startB+=1
elif A[startA]==B[startB]:
startA+=1
startB+=1
else:
startA+=1
return interval