Ugliest AC Python Solution


  • 0
    U
    class Solution(object):
        def summaryRanges(self, nums):
            if not nums:
                return []
            if len(nums) == 1:
                return [str(nums[0])]
            
            if len(nums) == 2:
                if nums[1] - nums[0] == 1:
                    return [str(nums[0]) + "->" + str(nums[1])]
                else:
                    return [str(nums[0]), str(nums[1])]
            
            low = nums[0]
            high = nums[0]
            res = []
            f = 0
            for i in range(1, len(nums)):
                if nums[i] - nums[i-1] == 1:
                    high = nums[i]
                    f = 1
                    continue
                else:
                    f = 0
                    if low == high:
                        res.append(str(low))
                    else:
                        res.append(str(low) + "->" + str(high))
                    low = nums[i]
                    high = nums[i]
            
            if f:
                res.append(str(low) + "->" + str(high))
            
            if nums[len(nums)-1] - nums[len(nums)-2] >1:
                res.append(str(nums[-1]))
            return res
        
    

Log in to reply
 

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