aliceyangxi1987的博客

智能时代,做个终身学习者,持续迭代;公众号-极客X养成计划;简书-不会停的蜗牛...

【LEETCODE】228-Summary Ranges

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
        


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/aliceyangxi1987/article/details/50329627
文章标签: LEETCODE PYTHON
个人分类: LEETCODE
上一篇【LEETCODE】204-Count Primes
下一篇【LEETCODE】27-Remove Element
想对作者说点什么? 我来说一句

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

关闭
关闭