-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path228.py
More file actions
27 lines (25 loc) · 834 Bytes
/
228.py
File metadata and controls
27 lines (25 loc) · 834 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
class Solution:
def summaryRanges(self, nums):
ans=[]
l=len(nums)
if l==0:
return [""]
cur_inter=[]
for i in range(l):
if len(cur_inter)==0:
cur_inter.append(nums[i])
elif len(cur_inter)>0 and nums[i]-cur_inter[-1]==1:
cur_inter.append(nums[i])
else:
if len(cur_inter)==1:
ans.append(str(cur_inter[0]))
else:
ans.append(str(cur_inter[0])+'->'+str(cur_inter[-1]))
cur_inter=[nums[i]]
if len(cur_inter)==1:
ans.append(str(cur_inter[0]))
else:
ans.append(str(cur_inter[0])+'->'+str(cur_inter[-1]))
return ans
nums = [0,1,2,4,5,7]
print(Solution().summaryRanges(nums))