Simple python solution

  • 0
    class Solution(object):
    def findMissingRanges(self, nums, lower, upper):
        :type nums: List[int]
        :type lower: int
        :type upper: int
        :rtype: List[str]
        if len(nums)==0: return [str(lower) if lower==upper else str(lower)+'->'+str(upper)]
        for i in xrange(len(nums)):
            if i==0 and nums[i]>lower:
                res+=[str(lower) if nums[i]-lower==1 else str(lower)+'->'+str(nums[i]-1)]
            if nums[i]-nums[i-1]>1:
                res+=[str(nums[i]-1) if nums[i]-nums[i-1]==2 else str(nums[i-1]+1)+'->'+str(nums[i]-1)]
            if i==len(nums)-1 and nums[i]<upper:
                res+=[str(upper) if upper-nums[i]==1 else str(nums[i]+1)+'->'+str(upper)]
        return res

  • 0

    Very straightforward solution, just calculate each range between every gap in nums.

Log in to reply

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