-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16.cpp
47 lines (44 loc) · 889 Bytes
/
16.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
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>
#include<vector>
#include<map>
#include<string>
#include <algorithm>
using namespace std;
int abs(int a, int b) {
if ( a > b)
return a - b;
else
return b-a;
}
int threeSumClosest(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
int min = nums[0] + nums[1] + nums[2];
for(int i = 0; i + 2< nums.size(); ++i) {
if(i > 0 && nums[i -1] == nums[i])
continue;
int j = i + 1;
int k = nums.size() - 1;
while(j < k) {
int v = nums[i] + nums[j] + nums[k];
if(abs(v, target) < abs(min, target))
min = v;
if(v < target)
++j;
else if(v > target)
--k;
else {
return target;
}
}
}
return min;
}
int main(){
vector<int> s;
// s.push_back(-1);
// s.push_back(0);
//s.push_back(1);
//threeSum(s);
cout<<s.size();
return 1;
}