-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16.cpp
More file actions
31 lines (31 loc) · 968 Bytes
/
Copy path16.cpp
File metadata and controls
31 lines (31 loc) · 968 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
30
31
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
int closest_number = 0;
int difference = INT_MAX;
for(int i=0; i<nums.size(); i++){
int start = i + 1;
int end = nums.size()-1;
if(start == end) break;
int sum = 0;
int local_difference = INT_MAX;
int local_closest = 0;
while(start < end){
sum = nums[start] + nums[end];
if(sum > target-nums[i]) end--;
else if(sum < target-nums[i]) start++;
else return target;
if(abs(sum+nums[i]-target) < local_difference){
local_difference = abs(sum+nums[i]-target);
local_closest = sum+nums[i];
}
}
if(local_difference < difference){
closest_number = local_closest;
difference = local_difference;
}
}
return closest_number;
}
};