Given a sorted integer array without duplicates, return the summary of its ranges.
For example, given [0,1,2,4,5,7]
, return ["0->2","4->5","7"].
My code:
class Solution(object):
def summaryRanges(self, nums):
"""
:type nums: List[int]
:rtype: List[str]
"""
result = []
n = len(nums)
if n ==0:
return []
i =0
while i < n:
a= nums[i]
while i+1<n and nums[i]==nums[i+1]-1:
i +=1
if a != nums[i]:
result.append("%d"%a+'->'+"%d"%nums[i])
else:
result.append("%d"%a)
i+=1
return result