标签:span tor targe first class end 绝对值 含义 amp
1 //搞清楚各个变量的含义 2 //***忘记对数组进行排序,以至于一直卡在这里*** 3 class Solution 4 { 5 public: 6 int threeSumClosest(vector<int>& nums, int target) 7 { 8 int n = nums.size(); 9 int res = INT_MAX;//绝对值之差 10 int sum = 0;//每一组数更新的和 11 sort(nums.begin(),nums.end()); 12 for(int i = 0;i < n - 2;i ++) 13 { 14 if(i > 0 && nums[i] == nums[i - 1]) continue; 15 int first = nums[i]; 16 int l = i + 1; 17 int r = n - 1; 18 while(l < r) 19 { 20 int temp1 = first + nums[l] + nums[r];//求和 21 int temp2 = abs(first + nums[l] + nums[r] - target);//绝对值之差 22 if(temp1 < target) l++; 23 else if(temp1 > target) r--; 24 else return target; 25 if(res > temp2) 26 { 27 res = temp2; 28 sum = temp1; 29 } 30 } 31 } 32 return sum; 33 } 34 };
标签:span tor targe first class end 绝对值 含义 amp
原文地址:https://www.cnblogs.com/yuhong1103/p/12499174.html