-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path18.cpp
More file actions
executable file
·59 lines (59 loc) · 1.97 KB
/
18.cpp
File metadata and controls
executable file
·59 lines (59 loc) · 1.97 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
54
55
56
57
58
59
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
class Solution {
public:
vector<vector<int> > fourSum(vector<int>& nums, int target) {
vector<int> comb;
vector<int> et1;
vector<int> et2;
vector<vector<int> > answer;
for (int i = 0; i < nums.size();i++)
for (int j = i+1; j < nums.size();j++)
if (i != j ) {
comb.push_back(nums[i] + nums[j]);
et1.push_back(i);
et2.push_back(j);
}
int left = 0; int right = comb.size()-1;
sort(comb.begin(),comb.end());
while (left < right){
while (comb[left] + comb[right] == target){
int end = left;
while (comb[left] == comb[end])
end++;
int st = right;
while (comb[right] == comb[st])
st--;
for (int k = left; k<=end;k++)
for (int l = st; l<=right;l++){
if (et1[k] != et2[l] && et1[k] != et1[l] && et2[k] != et2[l] && et2[k] != et1[l]){
vector<int> mid;
mid.push_back(nums[et1[k]]);
mid.push_back(nums[et2[k]]);
mid.push_back(nums[et1[l]]);
mid.push_back(nums[et2[l]]);
answer.push_back(mid);
}
}
left = end + 1;
right = st - 1;
}
if (comb[left] < target - comb[right])
left ++;
else right--;
}
for (int i = 0; i < answer.size();i++)
for (int j = 0; j < answer[i].size();j++)
cout<<answer[i][j]<<endl;
return answer;
}
};
int main(){
int al[6] = {1,0,-1,0,-2,2};
vector<int> alv(al,al+5);
Solution s;
s.fourSum(alv,0);
}