Short and Straightforward Python using 2 pointers O(n)


  • 0
        def summaryRanges(self, nums):
            """
            :type nums: List[int]
            :rtype: List[str]
            """
            if not nums: return []
            i, j, mn, mx, rList = 0, 1, 0, 0, []
            while j < len(nums):
                mn, jo = nums[i], j
                while j < len(nums) and nums[j-1] + 1 == nums[j]:
                    mx = nums[j]; j += 1
                if jo == j: rList.append(str(mn))
                else: rList.append(str(mn)+"->"+str(mx))
                i, j = j, j+1
            if i < len(nums) and i == j-1: rList.append(str(nums[i]))
            return rList
    

Log in to reply
 

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