-
Notifications
You must be signed in to change notification settings - Fork 614
/
163.py
43 lines (37 loc) · 1.06 KB
/
163.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'''
Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], return its missing ranges.
For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99, return ["2", "4->49", "51->74", "76->99"].
'''
class Solution(object):
def missingRange(self, A, lower, upper):
if not A:
return []
result = []
if A[0] != lower:
end = A[0] - 1
if end == lower:
m_r = str(lower)
else:
m_r = str(lower) + "->" + str(end)
result.append(m_r)
for index in range(1, len(A)):
if A[index] != A[index-1] + 1:
start = A[index-1] + 1
end = A[index] - 1
if start == end:
m_r = str(start)
else:
m_r = str(start) + "->" + str(end)
result.append(m_r)
if A[len(A) - 1] != upper:
start = A[len(A)-1] + 1
if start == upper:
m_r = str(start)
else:
m_r = str(start) + "->" + str(upper)
result.append(m_r)
return result
solution = Solution()
print solution.missingRange([0, 1, 3, 50, 75], 0, 99)
print solution.missingRange([4, 10, 50, 98], 0, 99)
print solution.missingRange([0], 0, 1)