AC Python solution, beat 48%, easy to understand


  • 0
    W

    class Solution(object):
    def summaryRanges(self, nums):
    """
    :type nums: List[int]
    :rtype: List[str]
    """
    lenNums = len(nums)
    result = []
    if lenNums == 0:
    return []
    elif (lenNums == 1):
    result.append(str(nums[0]))
    return result

        #print "lenNums = ", lenNums
        
    
        
        seperateIdx = 0
        while (seperateIdx <= lenNums - 1):
            startIdx = seperateIdx
            #print "1 seperateIdx = ", seperateIdx
            
            while( (seperateIdx+1 < lenNums) and (nums[seperateIdx] == (nums[seperateIdx+1] - 1))):
                seperateIdx +=1
            
            #print "2 seperateIdx = ", seperateIdx
            if startIdx == seperateIdx:
                #print "seperateIdx is 0, need to append ", str(nums[0])
                result.append(str(nums[startIdx]))
                    
                    
            else:
                #print "seperateIdx is not 0, need to append ", str(nums[seperateIdx])
                result.append(str(nums[startIdx]) + "->" + str(nums[seperateIdx]))
        
        
            seperateIdx +=1
    
        #print "result = ", result
        return result

Log in to reply
 

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