Skip to content

Commit

Permalink
2020-04-16
Browse files Browse the repository at this point in the history
  • Loading branch information
JiayangWu committed Apr 16, 2020
1 parent 0886f30 commit a513c8c
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions 0056.合并区间/0056-合并区间.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

return res

0 comments on commit a513c8c

Please sign in to comment.