zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    常用的逻辑控制器。
    map集合迭代。
    [译]如何在.NET Core中使用System.Drawing?
    使用.Net Core 2.1开发Captcha图片验证码服务
    ife 零基础学院 day 2
    ife 零基础学院 day 1
    Visual Studio 2017
    Visual Studio 2017
    在Windows 10上利用seafile搭建个人云服务
    Web网站配置Gzip,压缩js css文件
  • 原文地址:https://www.cnblogs.com/zhuozige/p/12774914.html
Copyright © 2011-2022 走看看