-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3SumClosest.cc
86 lines (65 loc) · 1.72 KB
/
3SumClosest.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <vector>
#include <string>
#include <iostream>
using std::vector;
using std::string;
using std::cout;
using std::endl;
#include <cstdlib>
using std::atoi;
struct ListNode{
int val;
ListNode *next;
ListNode(int x):val(x), next(NULL){}
};
#include <algorithm>
#include <cstring>
#include <map>
class Solution{
public:
typedef vector<int>::size_type sz;
int bsearch(vector<int> &num, sz low, sz high, int target){
if(low > high){
return 0x3f3f3f3f;
}
while(low <= high){
auto mid = (low + high) >> 1;
if(target == num[mid]){
return target;
}else if(target < num[mid]){
high = mid - 1;
}else{
low = mid + 1;
}
}
return num[low];
}
int abs(int x){
return x > 0? x: -x;
}
int threeSumClosest(vector<int> &num, int target){
std::sort(num.begin(), num.end());
int ans = 0x3f3f3f3f;
for(vector<int>::size_type i = 0; i < num.size(); ++i){
for(auto j = i + 1; j < num.size(); ++j){
auto tmp = num[i] + num[j];
auto rest = target - tmp;
auto fit = bsearch(num, j + 1, num.size() - 1, rest);
tmp += fit;
if(abs(target-tmp) < abs(target-ans)){
//cout << num[i] << " " << num[j] << " " << fit << endl;
ans = tmp;
}
}
}
return ans;
}
};
int main(){
Solution slu;
using std::cin;
//vector<int> num = {-1, 0, 1, 2, -1, -4};
vector<int> num = {-1, 2, 1, -4};
cout << slu.threeSumClosest(num, 1) << endl;
return 0;
}