9 lines - very simple and easy to understand Python solution


  • 0
    A
    class Solution(object):
        def findMissingRanges(self, nums, lower, upper):
            """
            :type nums: List[int]
            :type lower: int
            :type upper: int
            :rtype: List[str]
            """
            res = []
            nums = [lower - 1] + list(set(nums)) + [upper + 1]
            nums.sort()
            for i in range(1, len(nums)):
                if nums[i] - nums[i - 1] == 2:
                    res.append(str(nums[i] - 1))
                elif nums[i] - nums[i - 1] > 2 :
                    res.append(str(nums[i - 1] + 1) + '->' + str(nums[i] - 1))
            return res
    

Log in to reply
 

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