My Recursive Solution (Python)


  • 0
    A
    class Solution(object):
    def summaryRanges(self, nums):
        """
        :type nums: List[int]
        :rtype: List[str]
        """
        return self.summary(nums)
    def summary(self, nums):
        if not nums: return []
        if len(nums) == 1: return [str(nums[0])]
        ln = len(nums)
        low = 0
        i = 0
        while i<ln-1 and nums[i+1] == nums[i]+1:
            i+=1
        if i-low >=1:
            lr = str(nums[low])
            ur = str(nums[i])
            op = lr+"->"+ur
        else:
            op = str(nums[low])
        n = i+1
        nums = nums[n:]
        return [op] + self.summary(nums)

Log in to reply
 

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