和上一题一样的思路
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
sort(nums.begin(),nums.end());
int ans;
int mm=999999;
for(int i=0;i<nums.size();i++)
{
if(i!=0&&nums[i]==nums[i-1])
continue;
int start=i+1;
int end=nums.size()-1;
int x=nums[i];
while(start<end)
{
if(nums[end]+nums[start]+x>target)
{
if(mm>nums[end]+nums[start]+x-target)
{
mm=nums[end]+nums[start]+x-target;
ans=nums[end]+nums[start]+x;
}
end--;
continue;
}
else if(nums[end]+nums[start]+x<target)
{
if(mm>target-(nums[end]+nums[start]+x))
{
mm=target-(nums[end]+nums[start]+x);
ans=nums[end]+nums[start]+x;
}
start++;
continue;
}
else
{
return target;
}
}
}
return ans;
}
};