标签:bsp class 答案 解题思路 不能 一个 分享 http 遍历
题目如下:
解题思路:简单的不能再简单的题目了,对于任意一个A[i]来说,其可能的最小的最大值是A[i]-K,最大的最小值是A[i]+K。遍历数组,求出所有元素中最大的最小值和最小的最大值,两者之差(小于零则取零)就是答案。
代码如下:
class Solution(object): def smallestRangeI(self, A, K): """ :type A: List[int] :type K: int :rtype: int """ minv,maxv = A[0] + K, A[0] - K for i in A: minv = min(minv,i+K) maxv = max(maxv,i-K) return max(0,maxv-minv)
【leetcode】908. Smallest Range I
标签:bsp class 答案 解题思路 不能 一个 分享 http 遍历
原文地址:https://www.cnblogs.com/seyjs/p/9692725.html