Skip to content

Commit 9117928

Browse files
committed
solution: 0228. Summary Ranges
1 parent 18059ff commit 9117928

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

solutions/solution_0228/__init__.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
class Solution:
22
def summaryRanges(self, nums: list[int]) -> list[str]:
3-
pass
3+
if not nums:
4+
return []
5+
ranges = []
6+
start = end = nums[0]
7+
for i in range(1, len(nums)):
8+
if nums[i] == end + 1:
9+
end = nums[i]
10+
else:
11+
ranges.append(f'{start}->{end}' if start != end else str(start))
12+
start = end = nums[i]
13+
ranges.append(f'{start}->{end}' if start != end else str(start))
14+
return ranges

0 commit comments

Comments
 (0)