-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path#0228 Summary Ranges.py
More file actions
41 lines (32 loc) · 1.26 KB
/
#0228 Summary Ranges.py
File metadata and controls
41 lines (32 loc) · 1.26 KB
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
36
37
38
39
40
41
from typing import List
class Solution:
def summaryRanges(self, nums: List[int]) -> List[str]:
"""
Summary ranges for a sorted list of unique integers.
Given a sorted list of unique integers, this method returns the smallest
sorted list of ranges that cover all the numbers in the array exactly.
A range is represented in the format "a->b" if a != b, and "a" if a == b.
Args:
nums (List[int]): The input list of sorted unique integers.
Returns:
List[str]: A list of strings representing the summary ranges.
"""
if not nums:
return []
ranges = []
start = nums[0]
for i in range(1, len(nums)):
# If the current number is not consecutive, finalize the current range
if nums[i] != nums[i - 1] + 1:
if start == nums[i - 1]:
ranges.append(f"{start}")
else:
ranges.append(f"{start}->{nums[i - 1]}")
# Start a new range
start = nums[i]
# Finalize the last range
if start == nums[-1]:
ranges.append(f"{start}")
else:
ranges.append(f"{start}->{nums[-1]}")
return ranges