題目
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"].
思路
用兩個(gè)指針來(lái)確定一個(gè)區(qū)間
代碼
class Solution(object):
def summaryRanges(self, nums):
"""
:type nums: List[int]
:rtype: List[str]
"""
res = []
l = 0
while l < len(nums):
r = l
while r+1<len(nums) and (nums[r+1]-nums[r])==1:
r += 1
if l == r:
res.append(str(nums[l]))
l += 1
else:
res.append(str(nums[l])+'->'+str(nums[r]))
l = r+1
return res