题目链接
https://leetcode-cn.com/problems/summary-ranges/
代码实现
class Solution:
def summaryRanges(self, nums: List[int]) -> List[str]:
res = []
if not nums:
return []
start_pos = 0
nums.append(nums[-1]-1)
for i in range(1,len(nums)):
if nums[i] != nums[i-1] + 1:
if start_pos == i-1:
res.append("%d"%(nums[i-1],))
else:
res.append("%d->%d"%(nums[start_pos],nums[i-1]))
start_pos=i
return res