-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path228.py
More file actions
33 lines (29 loc) · 841 Bytes
/
228.py
File metadata and controls
33 lines (29 loc) · 841 Bytes
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
from typing import List
class Solution:
def summaryRanges(self, nums: List[int]) -> List[str]:
parts = []
for n in nums:
if not parts or n != parts[-1][-1] + 1:
parts.append([n])
else:
parts[-1].append(n)
res = []
for p in parts:
if len(p) == 1:
res.append(str(p[0]))
else:
res.append('{}->{}'.format(p[0], p[-1]))
return res
if __name__ == '__main__':
cases = [
([0,1,2,4,5,7], ["0->2","4->5","7"]),
([0,2,3,4,6,8,9], ["0","2->4","6","8->9"]),
([], []),
([-1], ["-1"]),
([0], ["0"]),
]
sln = Solution()
for nums, golden in cases:
res = sln.summaryRanges(nums)
print(golden, res)
assert golden == res