-
Notifications
You must be signed in to change notification settings - Fork 287
/
Copy path3Sum_Closest.cpp
34 lines (33 loc) · 1.15 KB
/
3Sum_Closest.cpp
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
30
31
32
33
34
//Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target.
//Return the sum of the three integers.
//You may assume that each input would have exactly one solution.
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int n = nums.size();
int ans = INT_MAX;
int diff = INT_MAX;
sort(nums.begin(),nums.end());
for(int i=0; i<n; i++){
int low = i+1;
int high = n-1;
int sum = target - nums[i];
while(low < high){
int two_sum = nums[low] + nums[high];
if(abs((two_sum + nums[i]) - target) < diff){
diff = abs((two_sum + nums[i]) - target);
ans = two_sum + nums[i];
}
if(two_sum > sum)
high--;
else if(two_sum < sum)
low++;
else
return (two_sum + nums[i]);
}
while(i + 1 < n && nums[i] == nums[i+1])
i++;
}
return ans;
}
};