-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0228.py
More file actions
23 lines (22 loc) · 690 Bytes
/
0228.py
File metadata and controls
23 lines (22 loc) · 690 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution:
def summaryRanges(self, nums: List[int]) -> List[str]:
if not nums:
return None
if len(nums) == 1:
return [str(nums[0])]
res = []
tmp = [str(nums[0])]
for i in range(1, len(nums)):
if nums[i] == nums[i-1] + 1:
if len(tmp) > 1:
tmp.pop()
tmp.append(str(nums[i]))
else:
if tmp:
res.append("->".join(tmp))
tmp = [str(nums[i])]
else:
tmp.append(str(nums[i]))
if tmp:
res.append("->".join(tmp))
return res