Simple Python Solution - first trial pass


  • 0
    This post is deleted!

  • 0
    class Solution(object):
        def summaryRanges(self, nums):
            """
            :type nums: List[int]
            :rtype: List[str]
            """
            if len(nums) == 0:
                return []
            if len(nums) == 1:
                return [str(nums[0])]
            res = []
            ele = ''
            for i in xrange(len(nums) - 1):
                if ele != '':
                    if nums[i+1] != nums[i] + 1:
                        ele += '->'
                        ele += str(nums[i])
                        res.append(ele)
                        ele = ''
                else:
                    if nums[i+1] != nums[i] + 1:
                        res.append(str(nums[i]))
                    else:
                        ele += str(nums[i])
            if ele == '':
                res.append(str(nums[-1]))
            else:
                ele += '->'
                ele += str(nums[-1])
                res.append(ele)
            return res
    

Log in to reply
 

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