910. 最小差值 II
910. 最小差值 II
题目链接:910. 最小差值 II
代码如下:
class Solution
{
public:int smallestRangeII(vector<int>& nums, int k) {sort(nums.begin(), nums.end());int res=nums.back()-nums.front();for (int i = 1; i < nums.size(); i++){int mx = max(nums[i - 1] + k, nums.back() - k);int mn= min(nums[i] - k, nums[0] + k);res = min(res, mx - mn);}return res;}
};