Skip to content

Commit 0bb1826

Browse files
committed
Time: 47 ms (53.98%), Space: 16.3 MB (75.52%) - LeetHub
1 parent b0bcf65 commit 0bb1826

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def summaryRanges(self, nums: List[int]) -> List[str]:
3+
if not nums:
4+
return []
5+
6+
ranges = []
7+
start = end = nums[0]
8+
9+
for i in range(1, len(nums)):
10+
if nums[i] == end + 1:
11+
end = nums[i]
12+
else:
13+
if start == end:
14+
ranges.append(str(start))
15+
else:
16+
ranges.append(str(start) + "->" + str(end))
17+
start = end = nums[i]
18+
19+
if start == end:
20+
ranges.append(str(start))
21+
else:
22+
ranges.append(str(start) + "->" + str(end))
23+
24+
return ranges

0 commit comments

Comments
 (0)