LeetCode 986
使用两个指针,检查是否有overlap的部分,如果有,加入结果。然后把其中相对小的值的指针一个向前移动一格。
def intervalIntersection(self, A: List[List[int]], B: List[List[int]]) -> List[List[int]]:
ap = 0
bp = 0
result = []
while ap < len(A) and bp < len(B):
start = max(A[ap][0], B[bp][0])
end = min(A[ap][1], B[bp][1])
#如果有相交部分,加入结果
if (start <= end):
result.append([start, end])
#较小值的指针向前移动一格
if A[ap][1] < B[bp][1]:
ap +=1
else:
bp +=1
return result