-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathq0228.py
More file actions
30 lines (27 loc) · 838 Bytes
/
q0228.py
File metadata and controls
30 lines (27 loc) · 838 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
#!/usr/bin/python3
from typing import List
class Solution:
def summaryRanges(self, nums: List[int]) -> List[str]:
length = len(nums)
if length == 0:
return []
if length == 1:
return [str(nums[0])]
result = []
start = 0
end = 0
for i in range(1, length):
if nums[i] - nums[start] == i - start:
end = i
else:
if end > start:
result.append(str(nums[start]) + "->" + str(nums[end]))
else:
result.append(str(nums[start]))
start = i
end = i
if end > start:
result.append(str(nums[start]) + "->" + str(nums[end]))
else:
result.append(str(nums[start]))
return result