10 lines concise python solution. consider two number every time.


  • 0
    D
    class Solution(object):
    def findMissingRanges(self, nums, lower, upper):
        """
        :type nums: List[int]
        :type lower: int
        :type upper: int
        :rtype: List[str]
        """
        
        nums = [lower-1] + nums + [upper+1]
        res = []
        # Deal with first
        pre = nums.pop(0)
        for cur in nums:
            if cur - pre == 2:
                res.append(str(pre+1))
            elif cur - pre > 2:
                res.append(str(pre+1) + '->' + str(cur-1))
            pre = cur
            
        return res
    

    Do not see same think, just post it here.
    Key point is that the result just contain two situations, with '->' and without '->', just check the next and previous at the same time.
    Also add lower-1 and upper+1 to list can make it simpler.


Log in to reply
 

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