LeetCode(力扣)56. 合并区间Python

LeetCode56. 合并区间

    • 题目链接
    • 代码

题目链接

https://leetcode.cn/problems/merge-intervals/description/
LeetCode(力扣)56. 合并区间Python_第1张图片

代码

class Solution:
    def merge(self, intervals: List[List[int]]) -> List[List[int]]:
        result = []
        if len(intervals) == 0:
            return result
        intervals.sort(key=lambda x: x[0])
        result.append(intervals[0])

        for i in range(1, len(intervals)):
            if result[-1][1] >= intervals[i][0]:
                result[-1][1] = max(result[-1][1], intervals[i][1])
            else:
                result.append(intervals[i])
        return result

你可能感兴趣的:(leetcode,python,算法,职场和发展)