228. Summary Ranges
https://leetcode.com/problems/summary-ranges/
Python
Two Pointer
class Solution:
def summaryRanges(self, nums: List[int]) -> List[str]:
length = len(nums)
if length < 2:
return [str(num) for num in nums]
cur, fast = 0, 0
result = []
while fast < length - 1:
if nums[fast] + 1 == nums[fast+1]:
fast += 1
continue
result.append(
"{}->{}".format(nums[cur], nums[fast]) if cur != fast
else str(nums[fast])
)
fast += 1
cur = fast
result.append(
"{}->{}".format(nums[cur], nums[fast]) if cur != fast
else str(nums[fast])
)
return result