Python Two Pointers O(n)


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

Log in to reply
 

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