今天看啥  ›  专栏  ›  hhh江月

leetcode刷题二十

hhh江月  · CSDN  ·  · 2021-01-01 00:00

leetcode刷题二十

题目叙述

给你一个下标从 0 开始的整数数组 nums ,该数组的大小为 n ,请你计算 nums[j] - nums[i] 能求得的 最大差值 ,其中 0 <= i < j < n 且 nums[i] < nums[j] 。

返回 最大差值 。如果不存在满足要求的 i 和 j ,返回 -1 。

题目解答

class Solution:
    def maximumDifference(self, nums: List[int]) -> int:
        ini = []
        for i in nums:
            ini.append(i)
        ini.sort()
        ini.reverse()
        out = 0
        if ini == nums:
            return -1
        for i in range(len(nums)):
            for j in range(i + 1, len(nums)):
                if nums[i] < nums[j]:
                    if nums[j] - nums[i] > out:
                        out = nums[j] - nums[i]
        return out



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19



原文地址:访问原文地址
快照地址: 访问文章快照