题目Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and add x to A[i].After this process, we have some array B.Return the smallest possible difference between the maximum value of B and the minimum value of B.Example 1:Input: A = [1], K = 0Output: 0Explanation: B = [1]Example 2:Input: A = [0,10], K = 2Output: 6Explanation: B = [2,8]Example 3:Input: A = [1,3,6], K = 3Output: 0Explanation: B = [3,3,3] or B = [4,4,4]Note:1 <= A.length <= 100000 <= A[i] <= 100000 <= K <= 10000题目地址讲解这道题刚开始一直没看懂题,但是看懂了之后就会发现非常非常简单。题目的意思是对一个数组A中的每一个数,都加一个x,x的取值范围是-K <= x <= K,然后得到结果数组B,取B中的最大值和最小值,让这两者的差最小(最小是0),求这个最小值。这里有个误区是所有数加的x都相同,但你仔细看题目给的例子,x并不是要相同的。看完例子我们发现,只要A的最大和最小值之差小于2倍K,我们就能让B中的元素全部相等。这就是解题思路。Java代码class Solution { public int smallestRangeI(int[] A, int K) { int max=A[0]; int min=A[0]; for(int i=0;i<A.length;i++){ if(max<A[i]){ max = A[i]; } if(min>A[i]){ min=A[i]; } } if(2K>max-min){ return 0; }else{ return max-min-2K; } }}