```
int threeSumClosest(vector<int> &num, int target) {
int result = INT_MAX - 1;
sort(num.begin(), num.end());
for(int i = 0; i < num.size();){
int start = i + 1;
int end = num.size() - 1;
while(start < end){
int sum = num[i] + num[start] + num[end];
if(sum == target){
return target;
}
else if(sum > target){
result = abs(sum - target) < abs(result - target) ? sum : result;
end--;
}
else{
result = abs(sum - target) < abs(result - target) ? sum : result;
start++;
}
}
int iNum = num[i];
while(i < num.size() && num[i] == iNum){
i++;
}
}
return result;
}
```

This solution is straightforward and has similar idea with 3sum. If you can solve 3sum but fail this problem, just review your 3sum code then you will make it.

Please notice the first line: int result = INT_MAX - 1; If I change it to int result = INT_MAX, the result is wrong. Anybody have any idea why this happens?