-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0228_Summary_Ranges.py
More file actions
27 lines (26 loc) · 915 Bytes
/
0228_Summary_Ranges.py
File metadata and controls
27 lines (26 loc) · 915 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: List[int]) -> List[str]:
sol = []
temp_num_left = 0
temp_num_right = 0
if len(nums) > 0:
temp_num_left = nums.pop(0)
temp_num_right = temp_num_left
else:
return []
while nums:
num = nums.pop(0)
if not num == temp_num_right + 1:
if not temp_num_left == temp_num_right:
sol.append(str(temp_num_left) + "->" + str(temp_num_right))
else:
sol.append(str(temp_num_left))
temp_num_left = num
temp_num_right = num
else:
temp_num_right = num
if temp_num_left == temp_num_right:
sol.append(str(temp_num_left))
else:
sol.append(str(temp_num_left) + "->" + str(temp_num_right))
return sol