-
Notifications
You must be signed in to change notification settings - Fork 171
/
163.py
19 lines (19 loc) · 794 Bytes
/
163.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution:
def findMissingRanges(self, nums, lower, upper):
if not nums:
return [str(lower) + "->" + str(upper)] if lower != upper else [str(lower)]
res, n = [], len(nums)
if lower + 1 < nums[0]:
res.append(str(lower) + "->" + str(nums[0] - 1))
elif lower + 1 == nums[0]:
res.append(str(lower))
for i in range(1, n):
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))
if nums[-1] + 1 < upper:
res.append(str(nums[-1] + 1) + "->" + str(upper))
elif nums[-1] + 1 == upper:
res.append(str(upper))
return res