-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path16.cpp
More file actions
executable file
·53 lines (53 loc) · 1.58 KB
/
16.cpp
File metadata and controls
executable file
·53 lines (53 loc) · 1.58 KB
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
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
vector<vector<int> > resVec;
sort(nums.begin(),nums.end());
vector<int> reala;
int answer = target + 100000;
for(int p1=0;p1<nums.size();p1++){
if(p1>0 && nums[p1] == nums[p1-1])
continue;
int p2 = p1+1;//begin
int p3 = nums.size()-1;//end
while(p2<p3){
int a = nums[p1];
int b = nums[p2];
int c = nums[p3];
if (abs(target-(a+b+c)) < abs(answer-target)){
answer = a+b+c;
reala.push_back(a);
reala.push_back(b);
reala.push_back(c);
}
if(b+c== -a + target){
vector<int> tempVec(3);
tempVec[0] = a;
tempVec[1] = b;
tempVec[2] = c;
resVec.push_back(tempVec);
while (p2 < p3 && nums[p2] == nums[p2 + 1])
p2++;
while (p2 < p3 && nums[p3 - 1] == nums[p3])
p3--;
p2++;
p3--;
}
else if(b+c < -a + target){
p2++;
}
else{
p3--;
}
}
}
return answer;
}
};
int main(){
}