Skip to content

Commit 2f7fca3

Browse files
authored
Refactor Summary Ranges - Leetcode 228.py: Use f-strings and simplify logic
f-strings make the code more readable and stored the len(nums) to avoid constant len() calls.
1 parent fcc230c commit 2f7fca3

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

Summary Ranges - Leetcode 228/Summary Ranges - Leetcode 228.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@ class Solution:
22
def summaryRanges(self, nums: List[int]) -> List[str]:
33
ans = []
44
i = 0
5-
6-
while i < len(nums):
5+
n = len(nums)
6+
while i < n:
77
start = nums[i]
8-
while i < len(nums)-1 and nums[i] + 1 == nums[i + 1]:
8+
while i < n - 1 and nums[i] + 1 == nums[i + 1]:
99
i += 1
1010

11-
if start != nums[i]:
12-
ans.append(str(start) + "->" + str(nums[i]))
13-
else:
14-
ans.append(str(nums[i]))
11+
ans.append(f"{start}->{nums[i]}" if start != nums[i] else f"{nums[i]}")
1512

1613
i += 1
1714

0 commit comments

Comments
 (0)