A clear Python solution (beat 95%)


  • 0
    S
    class Solution(object):
        def summaryRanges(self, nums):
            """
            :type nums: List[int]
            :rtype: List[str]
            """
            result = []
            i = 0
            while i < len(nums):
                tmp = []
                tmp.append(str(nums[i]))
                while i < len(nums)-1 and nums[i+1] == nums[i] + 1:
                    i += 1
                tmp.append(str(nums[i]))
                i += 1
                if tmp[0] != tmp[1]:
                    result.append("->".join(tmp))
                else:
                    result.append(str(tmp[0]))
            return result
    

Log in to reply
 

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