Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add solution for Summary Ranges
  • Loading branch information
jelonmusk committed Nov 24, 2024
commit 2bf1f7b1089d3968eabddae7a2cbe4ec57183fb6
23 changes: 23 additions & 0 deletions python/0228-Summary-Ranges.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution:
def summaryRanges(self, nums: List[int]) -> List[str]:
result = []
n = len(nums)
if n == 0:
return result

# Pointer to the start of the current range
start = 0

for i in range(1, n + 1): # Iterate until the end of the array
# If we're at the end of the array or the range breaks
if i == n or nums[i] != nums[i - 1] + 1:
# Single number range
if start == i - 1:
result.append(str(nums[start]))
# Continuous range
else:
result.append(f"{nums[start]}->{nums[i - 1]}")
# Update start for the next range
start = i

return result