Best python solution, beats 100 % of python submissions !


  • 0
    S
    class Solution(object):
        def summaryRanges(self, nums):
            """
            :type nums: List[int]
            :rtype: List[str]
            """
            if not nums:
                return nums
            nums.sort()
            low = 0
            summary = []
            count = 0
            for i in range(1, len(nums)):
                if nums[i] == nums[i - 1] + 1:
                    count += 1
                else:
                    if count:
                        summary.append("{}->{}".format(nums[low], nums[i - 1])) 
                    else:
                        summary.append("{}".format(nums[low]))
                    low = i
                    count = 0
            if count:
                summary.append("{}->{}".format(nums[low], nums[i]))
            else:
                summary.append("{}".format(nums[low]))
            return summary
                    
    

Log in to reply
 

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