-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem228.py
More file actions
31 lines (28 loc) · 791 Bytes
/
problem228.py
File metadata and controls
31 lines (28 loc) · 791 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
# <---coding: utf-8--->
# @Time: 2022/4/24/18:12
# @Author = posiondy
# @Email: 1547590574@qq.com
# @Software: PyCharm
from typing import List
class Solution:
def summaryRanges(self, nums: List[int]) -> List[str]:
r = []
res = []
for i in nums:
if len(res) == 0 or i == res[-1] + 1:
res.append(i)
else:
if len(res) == 1:
r.append(f"{res[-1]}")
else:
r.append(f"{res[0]}->{res[-1]}")
res.clear()
res.append(i)
if len(res) == 1:
r.append(f"{res[-1]}")
if len(res) > 1:
r.append(f"{res[0]}->{res[-1]}")
return r
s = Solution()
r = s.summaryRanges([0,1,2,4,5,7])
print(r)