【LEETCODE】228-Summary Ranges

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/aliceyangxi1987/article/details/50329627

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
        


阅读更多

没有更多推荐了,返回首页