Concise Python Solution with Explanation

• Just simply insert `lower-1` and `upper+1` into the list

The missing range should be `num[i]+1 ~ num[i+1]-1`

``````def findMissingRanges(self, nums, lower, upper):
"""
:type nums: List[int]
:type lower: int
:type upper: int
:rtype: List[str]
"""
nums.insert(0,lower-1)
nums.append(upper+1)
res = []
i = 0
while i<len(nums)-1:
left,right = nums[i],nums[i+1]
if left!=right-1:
if right-left==2:
res.append(str(right-1))
else:
res.append(str(left+1)+"->"+str(right-1))
i=i+1
return res``````

• @liuxiyun-nku It fails for the following testcase.
Input:
[1,1,1]
1
1

Your answer:
["2->0","2->0"]

Expected answer:
[]

• @pradeepcsekar The last else should change to `elif right-left>2:`

``````    def findMissingRanges(self, nums, lower, upper):
"""
:type nums: List[int]
:type lower: int
:type upper: int
:rtype: List[str]
"""
nums.insert(0,lower-1)
nums.append(upper+1)
res = []
i = 0
while i<len(nums)-1:
left,right = nums[i],nums[i+1]
if left!=right-1:
if right-left==2:
res.append(str(right-1))
elif right-left>2:
res.append(str(left+1)+"->"+str(right-1))
i=i+1
return res

``````

then we can see first `if` condition is not necessary.

``````    def findMissingRanges(nums, lower, upper):
"""
:type nums: List[int]
:type lower: int
:type upper: int
:rtype: List[str]
"""
nums.insert(0,lower-1)
nums.append(upper+1)
res = []
i = 0
while i<len(nums)-1:
left,right = nums[i],nums[i+1]
if right-left==2:
res.append(str(right-1))
elif right-left>2:
res.append(str(left+1)+"->"+str(right-1))
i=i+1
return res
``````

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