leetcode 56 合并区间
2020-02-27 本文已影响0人
Arsenal4ever
画个坐标轴,思路立有,直接贪心维护区间 start 和 end 就行了嘛
class Solution(object):
def merge(self, intervals):
"""
:type intervals: List[List[int]]
:rtype: List[List[int]]
"""
if not intervals:
return []
intervals.sort(key=lambda x: x[0])
start, end = intervals[0][0], intervals[0][1]
result = []
for item in intervals:
if item[0] <= end:
end = max(end, item[1])
else:
result.append([start, end])
start = item[0]
end = item[1]
result.append([start, end])
return result