题解 | #合并区间#
合并区间
http://www.nowcoder.com/practice/69f4e5b7ad284a478777cb2a17fb5e6a
!!!注意判空
!!!注意给定区间是否有序
# 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 ): # write code here if not intervals: return [] intervals = sorted(intervals, key = lambda interval: interval.start) curr = intervals[0] res = [] for i in range(1, len(intervals)): if curr.start <= intervals[i].start <= curr.end: curr.end = max(curr.end, intervals[i].end) else: res.append(curr) curr = intervals[i] res.append(curr) return res