27 lines
575 B
Java
27 lines
575 B
Java
/*
|
|
* @lc app=leetcode id=908 lang=java
|
|
*
|
|
* [908] Smallest Range I
|
|
*/
|
|
|
|
// @lc code=start
|
|
class Solution {
|
|
public int smallestRangeI(int[] nums, int k) {
|
|
if(nums == null || nums.length == 0) {
|
|
return 0;
|
|
}
|
|
int min = nums[0], max = nums[0];
|
|
for(int i = 1; i < nums.length; i++) {
|
|
if(nums[i] < min) {
|
|
min = nums[i];
|
|
}
|
|
if(nums[i] > max) {
|
|
max = nums[i];
|
|
}
|
|
}
|
|
return max - min > 2 * k ? max - min - 2 * k : 0;
|
|
}
|
|
}
|
|
// @lc code=end
|
|
|