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
  • 相关阅读:
    第02组 Alpha冲刺 (6/6)
    面向对象分析设计考试复习【历年卷】
    第02组 Alpha冲刺 (5/6)
    第02组 Alpha冲刺 (4/6)
    第02组 Alpha冲刺 (3/6)
    第02组 Alpha冲刺 (2/6)
    第02组 Alpha冲刺 (1/6)
    linux内核结构
    从别人的博客学习
    递归的认识
  • 原文地址:https://www.cnblogs.com/zhuozige/p/12774914.html
Copyright © 2011-2022 走看看