Summary Ranges

Problem Id: 228 Difficulty: Easy


Intuition

Solution


class Solution:
    def summaryRanges(self, nums: List[int]) -> List[str]:
        if not nums:
            return []
        start = end = nums.pop(0)
        nums.append([-1])

        result = []
        for num in nums:
            if start is None:
                start = end = num
            elif num == end + 1:
                end = num
            else:
                if start == end:
                    result.append(str(start))
                else:
                    result.append("%d->%d" % (start, end))
                start = end = num

        return result