Python O(n) - time & space


  • 3
    L
    class Solution:
        # @param {integer[]} nums
        # @return {string[]}
        def summaryRanges(self, nums):
            result = []
            if not nums: return result
            start = current = None
            for num in nums:
                if start == None:
                    start = current = num
                elif num == current + 1:
                    current = num
                else:
                    result.append(self.generateStringRange(start, current))
                    start = current = num
            result.append(self.generateStringRange(start, current))
            return result
        
        
        def generateStringRange(self, start, end):
            result = str(start)
            if start != end: result += "->" + str(end)
            return result

Log in to reply
 

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