Skip to content

Instantly share code, notes, and snippets.

@zhangxiaomu01
Created October 22, 2018 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangxiaomu01/bf455a9a614ed5de4c706bd9baafe563 to your computer and use it in GitHub Desktop.
Save zhangxiaomu01/bf455a9a614ed5de4c706bd9baafe563 to your computer and use it in GitHub Desktop.
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