leetcode-986

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
合并区间
# Definition for an interval.
class Interval(object):
def __init__(self, s=0, e=0):
self.start = s
self.end = e

class Solution(object):
def intervalIntersection(self, A, B):
"""
:type A: List[Interval]
:type B: List[Interval]
:rtype: List[Interval]
"""
a, b, res, i, j = len(A), len(B), [], 0, 0
while i < a and j < b:
left_a, right_a = A[i].start, A[i].end
left_b, right_b = B[j].start, B[j].end
if left_a < left_b:
left = left_b
else:
left = left_a
if right_a < right_b:
right = right_a
i += 1
else:
right = right_b
j += 1
if right >= left:
res.append(Interval(left, right))
return res