class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
vector<vector<int>> res;
int size = nums.size();
if (size < 3) {
return 0;
}
int value = INT_MAX, minSum = -1;
sort(nums.begin(), nums.end());
for (int i = 0; i < size; i++) {
int j = i + 1;
int k = size - 1;
while (j < k) {
int sum = nums[i] + nums[j] + nums[k];
int diff = sum - target;
if (diff == 0){
return sum;
} else if (diff > 0) {
k--;
} else {
j++;
}
if (abs(diff) < value) {
value = abs(diff);
minSum = sum;
}
}
}
return minSum;
}
};