228. Summary Ranges**

来源:互联网 发布:心动网络密保产品在哪 编辑:程序博客网 时间:2024/06/05 04:06

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"].

My code:

class Solution(object):     def summaryRanges(self, nums):         """         :type nums: List[int]         :rtype: List[str]         """         result = []         n = len(nums)         if n ==0:             return []         i =0         while i < n:             a= nums[i]             while i+1<n and nums[i]==nums[i+1]-1:                 i +=1             if a != nums[i]:                result.append("%d"%a+'->'+"%d"%nums[i])             else:                result.append("%d"%a)             i+=1         return result

Reference

0 0