Leetcode练习(Python):数组类:第228题:给定一个无重复元素的有序整数数组,返回数组区间范围的汇总。

题目:
给定一个无重复元素的有序整数数组,返回数组区间范围的汇总。
思路:
本题思路简单。
程序:
class Solution:
    def summaryRanges(self, nums: List[int]) -> List[str]:
        length = len(nums)
        if length <= 0:
            return []
        if length == 1:
            return [str(nums[0])]
        result = []
        head = 0
        for index in range(1, length):
            if nums[index] - nums[index - 1] != 1:
                tail = index - 1
                if head == tail:
                    result.append(str(nums[head]))
                else:
                    result.append(str(nums[head]) + '->' + str(nums[tail]))
                if index != length - 1:
                    head = index
                else:
                    result.append(str(nums[length - 1]))
            elif nums[index] - nums[index - 1] == 1 and index == length - 1:
                result.append(str(nums[head]) + '->' + str(nums[index]))
        return result
原文地址:https://www.cnblogs.com/zhuozige/p/12774914.html