Skip to content

Commit d667fbe

Browse files
authored
Create Summary Ranges - Leetcode 228.py
1 parent d6dadfe commit d667fbe

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Summary Ranges - Leetcode 228.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def summaryRanges(self, nums: List[int]) -> List[str]:
3+
ans = []
4+
i = 0
5+
6+
while i < len(nums):
7+
start = nums[i]
8+
while i < len(nums)-1 and nums[i] + 1 == nums[i + 1]:
9+
i += 1
10+
11+
if start != nums[i]:
12+
ans.append(str(start) + "->" + str(nums[i]))
13+
else:
14+
ans.append(str(nums[i]))
15+
16+
i += 1
17+
18+
return ans
19+
# Time: O(n)
20+
# Space: O(n)

0 commit comments

Comments
 (0)