题解 | 合并区间
# class Interval: # def __init__(self, a=0, b=0): # self.start = a # self.end = b # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param intervals Interval类一维数组 # @return Interval类一维数组 # class Solution: def merge(self , intervals: List[Interval]) -> List[Interval]: # write code here if intervals == []: return [] intervals.sort(key = lambda x:x.start) res = [intervals[0]] crt = 0 for i in range(1, len(intervals)): if res[crt].end >= intervals[i].start: res[crt].end = max(intervals[i].end, res[crt].end) else: res.append(intervals[i]) crt += 1 return res
经典排序+贪心