Python Solution in O(n)


  • 0
    R

    """

    def summaryRanges(self, arr):

        result = []
        start = 0
        end = 0
    
        while end <= len(arr)-1:
        #Increment the end pointer till new range
            if end!=len(arr)-1 and arr[end+1]-arr[end] == 1:
                if end == len(arr)-1:
                    break
            else:
               #add range to result
                temp = str(arr[start])+"->"+str(arr[end])
                #checks if range or single
                if arr[end] - arr[start] >= 1:
                    result.append(temp)
                else:
                    result.append(str(arr[end]))
                #start comes to the beginning of the new range
                start = end + 1
            end += 1
        return result
    

    """


Log in to reply
 

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