Simple and easy understand solution using python


  • 1
    W
    def generate_str(begin, end):
        if begin == end:
            return str(begin)
        else:
            return str(begin) + '->' + str(end)
    class Solution(object):
    
            
        def findMissingRanges(self, nums, lower, upper):
            """
            :type nums: List[int]
            :type lower: int
            :type upper: int
            :rtype: List[str]
            """
            n = len(nums)
            result = []
            if n == 0:
                result.append(generate_str(lower, upper))
                return result
    
            
            if nums[0] > lower:
                result.append(generate_str(lower, nums[0] - 1))
                
            for i in range(1, n):
                if nums[i] - nums[i-1] > 1:
                    result.append(generate_str(nums[i-1]+1, nums[i]-1))
                    continue
                    
            if nums[n-1] < upper:
                result.append(generate_str(nums[n-1] +1, upper))
            
            return result
                    
    

Log in to reply
 

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