leetcode 56 合并区间

作者: Arsenal4ever | 来源:发表于2020-02-27 22:04 被阅读0次

    画个坐标轴,思路立有,直接贪心维护区间 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
    

    相关文章

      网友评论

        本文标题:leetcode 56 合并区间

        本文链接:https://www.haomeiwen.com/subject/oqpfhhtx.html