6-line Python with two guards


  • 2
    Z

    Simplify the border cases with two guards.

    def findMissingRanges(self, nums, lower, upper):
        prev, res = lower, []
        for i in [lower-1]+nums+[upper+1]:
            if i - prev > 1:
                res += [str(prev+1)] if i-prev == 2 else [str(prev+1) + "->" + str(i-1)]
            prev = i
        return res

Log in to reply
 

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