My Simple Python Solution


  • 0
    class Solution(object):
        def summaryRanges(self, nums):
            if not nums:
                return []
            res, s_e, i = [], [nums[0], None], 0
            while i < len(nums)-1:
                if nums[i+1] - nums[i] == 1:
                    s_e[1] = nums[i+1]
                else:
                    res.append(self.getRange(s_e))
                    s_e = [nums[i+1], None]
                i += 1
            # append final item 
            res.append(self.getRange(s_e))
            return res
            
        def getRange(self, range_vals):
            if range_vals[1] == None:
                return str(range_vals[0])
            else:
                return str(range_vals[0]) + "->" + str(range_vals[1])
    

Log in to reply
 

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