-
Notifications
You must be signed in to change notification settings - Fork 64
/
3sum-closest.cpp
31 lines (30 loc) · 954 Bytes
/
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
// Time: O(n^2)
// Space: O(1)
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
sort(begin(nums), end(nums));
int result = 0, min_diff = numeric_limits<int>::max();
for (int i = size(nums) - 1; i >= 2; --i) {
if (i + 1 < size(nums) && nums[i] == nums[i + 1]) {
continue;
}
int left = 0, right = i - 1;
while (left < right) {
const auto& total = nums[left] + nums[right] + nums[i];
if (total < target) {
++left;
} else if (total > target) {
--right;
} else {
return target;
}
if (abs(total - target) < min_diff) {
min_diff = abs(total - target);
result = total;
}
}
}
return result;
}
};