Python ugly solution


  • 0
    S

    Following is my solution.
    Time: O(N)
    Space: O(N)

    class Solution(object):
        def summaryRanges(self, nums):
            """
            :type nums: List[int]
            :rtype: List[str]
            """
            n = len(nums)
            if n == 0: return nums
            elif n == 1: return [str(nums[0])]
            
            start, end, temp, res = nums[0], nums[0], [], []
            
            for i in range(1, n):
                if i == n-1:
                    if end + 1 != nums[i]:
                        temp.append([start, nums[i-1]])
                        temp.append([nums[i], nums[i]])
                        break
                    else:
                        temp.append([start, nums[i]])
                        break
                        
                if (end + 1 != nums[i]):
                    temp.append([start, nums[i-1]])
                    start=nums[i]
                end=nums[i]
                #print("i {}, start {}, end {}".format(i, start, end))
            for j in temp:
                if j[0] == j[1]:
                    res.append(str(j[0]))
                else:
                    res.append("{}->{}".format(j[0], j[1]))
    
            #print("temp : {}".format(temp))
            #print("res : {}".format(res))
            return res
    

Log in to reply
 

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