LeetCode 228 Summary Ranges (Python)

Posted by 小明MaxMing on October 28, 2020

题目

You are given a sorted unique integer array nums.

Return the smallest sorted list of ranges that cover all the numbers in the array exactly. That is, each element of nums is covered by exactly one of the ranges, and there is no integer x such that x is in one of the ranges but not in nums.

Each range [a,b] in the list should be output as:

  • “a->b” if a != b
  • “a” if a == b

解题思路

记录区间起点位置,如果后一个数与当前数字不相邻,则前一段形成一个区间,分别处理一个元素和多个元素的情况

代码

class Solution:
    def summaryRanges(self, nums: List[int]) -> List[str]:
        l = len(nums)
        if l == 0:
            return []
        nums.append(float('inf'))
        res, start = [], 0
        for i in range(l):
            if nums[i + 1] != nums[i] + 1:
                res.append(str(nums[i]) if i == start else "%s->%s" % (nums[start], nums[i]))
                start = i + 1
        return res

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅