Python Solution


  • 0
    class Solution(object):
        def findMissingRanges(self, nums, lower, upper):
            res = []
            nums = [lower-1] + nums + [upper+1]
            left_p = 0
            right_p = 1
            while right_p < len(nums):
                left = nums[left_p]
                right = nums[right_p]
                if left+1 == right-1:
                    res.append(str(left+1))
                elif (right-1) - (left+1) > 0:
                    res.append(str(left+1) + "->" + str(right-1))
                left_p += 1
                right_p += 1
            return res
    

    Insert lower-1 and upper+1 into the two ends of num. And just compare two numbers at a time.


Log in to reply
 

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