Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int sub = INT_MAX;
int sum = 0;
int len = nums.size();
for(int i = 0; i < len; i++){
for(int j = i+1; j < len; j++){
for(int k = j+ 1; k < len; k++){
if(abs(nums[i]+nums[j]+nums[k] - target) < sub){
sum = nums[i]+nums[j]+nums[k];
sub = abs(sum - target);
}
}
}
}
return sum;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment