Straight forward solution in Python


  • 0
    X
    class Solution(object):
        def summaryRanges(self, nums):
            """
            :type nums: List[int]
            :rtype: List[str]
            """
            n = len(nums); ans = []
            begin = 0; end = 0
            if n == 1: return [str(nums[0])]; tmp = ''
            while end <= n-1:
                while end < n-1 and nums[end]+1 == nums[end+1]:
                    end += 1
                if begin == end:
                    tmp = str(nums[begin])
                else:
                    tmp = str(nums[begin]) + '->' + str(nums[end])
                ans.append(tmp)
                begin = end + 1
                end = begin
            return ans

Log in to reply
 

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