-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path228_summary_ranges.py
More file actions
38 lines (36 loc) · 1.24 KB
/
Copy path228_summary_ranges.py
File metadata and controls
38 lines (36 loc) · 1.24 KB
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
class Solution(object):
def summaryRanges(self, nums):
"""
:type nums: List[int]
:rtype: List[str]
"""
ranges = []
if len(nums) > 0:
i, j = 0, 0
for j in range(1, len(nums)):
if abs(nums[j] - nums[j - 1]) == 1:
continue
if i == j - 1:
ranges.append('{}'.format(nums[i]))
else:
ranges.append('{}->{}'.format(nums[i],nums[j-1]))
i = j
if i == j:
ranges.append('{}'.format(nums[i]))
else:
ranges.append('{}->{}'.format(nums[i],nums[j]))
return ranges
vectors = [
[], [],
[0], ['0'],
[0,1,2,4,5,7], ['0->2','4->5','7'],
[0,2,3,4,6,8,9], ['0','2->4','6','8->9'],
[-100,-99,-98,-97,0,1,2,3,5,6,8,9,10,11,12,15], ['-100->-97','0->3','5->6','8->12','15'],
[0,2,4,6,8,10], ['0','2','4','6','8','10']
]
for i in range(0, len(vectors), 2):
nums = vectors[i]
expected = vectors[i + 1]
print(f'{nums} {expected}')
returned = Solution().summaryRanges(nums)
assert expected == returned, f'for {nums} expected {expected}, returned {returned}!'