Created
October 22, 2018 12:06
-
-
Save zhangxiaomu01/bf455a9a614ed5de4c706bd9baafe563 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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