228

 

 

 

 

 

#
# @lc app=leetcode.cn id=228 lang=python3
#
# [228] 汇总区间
#

# @lc code=start
class Solution:
    def summaryRanges(self, nums: List[int]) -> List[str]:
        if len(nums) <= 0:
            return []
        res = [str(nums[0])]
        for i in range(1,len(nums)):
            if nums[i] - int(res[-1].split('->')[-1]) == 1:
                res[-1] = res[-1].split('->')[0] + '->' + str(nums[i])
            else:
                res.append(str(nums[i]))
        return res
# @lc code=end

 

posted @ 2023-04-03 15:03  乐乐章  阅读(45)  评论(0编辑  收藏  举报