forked from iphkwan/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_Sum_Closest.cc
39 lines (39 loc) · 1.1 KB
/
3_Sum_Closest.cc
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
35
36
37
38
39
class Solution {
public:
int abs(int x, int y) {
if (x >= y) {
return x - y;
} else {
return y - x;
}
}
int threeSumClosest(vector<int> &num, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (num.size() < 3) {
return 0;
}
sort(num.begin(), num.end());
int ret = num[0] + num[1] + num[2];
int left, right, tmp;
for (int i = 0; i < num.size() - 2; i++) {
left = i + 1;
right = num.size() - 1;
while (left < right) {
tmp = num[i] + num[left] + num[right];
if (abs(target, tmp) < abs(target, ret)) {
ret = tmp;
if (ret == target) {
return ret;
}
}
if (tmp < target) {
left++;
} else {
right--;
}
}
}
return ret;
}
};