python 11 lines, elegantly handle edge cases


  • 0
    L

    Use lower - 1 and upper+1 to handle edge cases.

    def findMissingRanges(self, nums, lower, upper):
        l = lower-1
        ret = []
        nums.append(upper+1)
        for e in nums:
            if e - l == 2:
                ret.append(str(e-1))
            if e - l > 2:
                ret.append(str(l+1) + "->" + str(e-1) )
            l = e
        del nums[-1]
        return ret
    

Log in to reply
 

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