-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path58.py
More file actions
35 lines (27 loc) · 736 Bytes
/
58.py
File metadata and controls
35 lines (27 loc) · 736 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
32
33
34
35
"""
Time complexity: O(n)
- n: length of nums
"""
class Solution:
def summaryRanges(self, nums: List[int]) -> List[str]:
i, j = 0, 1
res = []
if len(nums) == 0:
return res
while True:
if j == len(nums):
if i == j - 1:
res.append(f"{nums[i]}")
else:
res.append(f"{nums[i]}->{nums[j-1]}")
break
if nums[j] - nums[i] == j - i:
j += 1
continue
if i == j - 1:
res.append(f"{nums[i]}")
else:
res.append(f"{nums[i]}->{nums[j-1]}")
i = j
j += 1
return res