Simple Readable Python Solution


  • 0
    M
        def summaryRanges(self, nums):
            """
            :type nums: List[int]
            :rtype: List[str]
            """
            ret = []
            i= 0
            while i < len(nums):
                start = i
                while i < len(nums) - 1 and nums[i+1] - nums[i] == 1:
                    i += 1
                if start != i:
                    ret.append('->'.join(map(str, [nums[start], nums[i]])))
                else:
                    ret.append(str(nums[start]))
                i += 1
                start = i
            return ret
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.