diff --git "a/0056.\345\220\210\345\271\266\345\214\272\351\227\264/0056-\345\220\210\345\271\266\345\214\272\351\227\264.py" "b/0056.\345\220\210\345\271\266\345\214\272\351\227\264/0056-\345\220\210\345\271\266\345\214\272\351\227\264.py" index 04c87a0..c005fbe 100644 --- "a/0056.\345\220\210\345\271\266\345\214\272\351\227\264/0056-\345\220\210\345\271\266\345\214\272\351\227\264.py" +++ "b/0056.\345\220\210\345\271\266\345\214\272\351\227\264/0056-\345\220\210\345\271\266\345\214\272\351\227\264.py" @@ -4,18 +4,21 @@ def merge(self, intervals): :type intervals: List[List[int]] :rtype: List[List[int]] """ - if not intervals: + if not intervals or not intervals[0]: return intervals - intervals = sorted(intervals, key = lambda x: x[0]) - start, end = intervals[0][0], intervals[0][1] + intervals = sorted(intervals, key = lambda x:x[0]) + res = [] - for i, interval in enumerate(intervals): - if interval[0] > end: - res.append([start, end]) - start, end = interval[0], interval[1] + start, end = intervals[0][0], intervals[0][1] + for interval in intervals: + s, e = interval[0], interval[1] + + if s <= end: # overlap + end = max(end, e) else: - end = max(end, interval[1]) + res.append([start, end]) + start, end = s, e + res.append([start, end]) - return res - \ No newline at end of file + return res \ No newline at end of file