def intervalIntersection(self, firstList, secondList):
"""
:type firstList: List[List[int]]
:type secondList: List[List[int]]
:rtype: List[List[int]]
"""
i,j = 0,0
res = []
while i < len(firstList) and j < len(secondList):;
l = max(firstList[i][0],secondList[j][0])
r = min(firstList[i][1],secondList[j][1])
if r - l >= 0:
res.append([l,r])
if firstList[i][1] < secondList[j][1]: #主要注意这里的更新,结束的较早的那个区间不可能与其他区间再有交集,所以可以向后移动
i += 1
else:
j += 1
return res
leetcode986. 区间列表的交集
最新推荐文章于 2024-07-25 15:58:50 发布