题解 | #合并区间#
合并区间
https://www.nowcoder.com/practice/69f4e5b7ad284a478777cb2a17fb5e6a
class Solution: def merge(self , intervals: List[Interval]) -> List[Interval]: # write code here if not intervals: return [] intervals.sort(key=lambda x : x.start) res = [intervals[0]] for i in range(1, len(intervals)): if intervals[i].end <= res[-1].end: continue elif intervals[i].start > res[-1].end: res.append(intervals[i]) else: res[-1].end = intervals[i].end return res