题目:
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"].
class Solution:
# @param {integer[]} nums
# @return {string[]}
def summaryRanges(self, nums):
ans = []
i = 0
while i<len(nums):
if i<len(nums)-1 and 1==nums[i+1]-nums[i]:
start = nums[i]
j = i+1
while(j<len(nums) and nums[j]-nums[i]==1):
i += 1
j += 1
end = nums[j-1]
ans.append(str(start)+"->"+str(end))
i = j
else:
ans.append(str(nums[i]))
i += 1
return ans