【LEETCODE】228-Summary Ranges

来源:互联网 发布:网络规划师不承认高工 编辑:程序博客网 时间:2024/05/16 23:34

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(object):    def summaryRanges(self, nums):        """        :type nums: List[int]        :rtype: List[str]        """                start=0        n=len(nums)        i=0        ans=[]                while i<n:            s=i            r=str(nums[i])            while i+1<n and nums[i+1]-nums[i]==1:                i+=1            if i>s:                r=r+"->"+str(nums[i])            ans.append(r)            i+=1        return ans        


0 0
原创粉丝点击