forked from larissalages/code_problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16.cpp
More file actions
29 lines (29 loc) · 990 Bytes
/
16.cpp
File metadata and controls
29 lines (29 loc) · 990 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
if(nums.size()<3) return 0;
vector<int> sortedNums;
copy(nums.begin(),nums.end(),back_inserter(sortedNums));
sort(sortedNums.begin(),sortedNums.end());
int ans,a,sum,diff,mindiff=INT_MAX,indexA,indexB,indexC;
for(int i=0;i<sortedNums.size()-2;i++){
indexA=i;
a=sortedNums[indexA];
if(indexA>0 && a==sortedNums[indexA-1]) continue;
indexB=i+1;
indexC=sortedNums.size()-1;
while(indexB<indexC){
sum=sortedNums[indexA]+sortedNums[indexB]+sortedNums[indexC];
diff=abs(target-sum);
if(diff==0) return sum;
if(diff<mindiff){
mindiff=diff;
ans=sum;
}
if(sum<target) indexB++;
else indexC--;
}
}
return ans;
}
};