• 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
  • 相关阅读:
    python之字典dict
    python之 tuple元组
    python之列表list
    数字图像处理
    深度神经网络优化
    神经网络的前向后向及更新
    0220 kd树(鸢尾花分类)
    024 查看数据库的编码格式
    208 MySQL性能分析之Explain
    207 MySQL索引的数据结构B+树介绍
  • 原文地址:https://www.cnblogs.com/zhuozige/p/12774914.html
Copyright © 2020-2023  润新知