We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent fcc230c commit 2f7fca3Copy full SHA for 2f7fca3
Summary Ranges - Leetcode 228/Summary Ranges - Leetcode 228.py
@@ -2,16 +2,13 @@ class Solution:
2
def summaryRanges(self, nums: List[int]) -> List[str]:
3
ans = []
4
i = 0
5
-
6
- while i < len(nums):
+ n = len(nums)
+ while i < n:
7
start = nums[i]
8
- while i < len(nums)-1 and nums[i] + 1 == nums[i + 1]:
+ while i < n - 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]))
+ ans.append(f"{start}->{nums[i]}" if start != nums[i] else f"{nums[i]}")
15
16
17
0 commit comments