-
Notifications
You must be signed in to change notification settings - Fork 46
/
4Sum.cpp
48 lines (41 loc) · 1.21 KB
/
4Sum.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
48
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
int n = nums.size();
sort(nums.begin(),nums.end());
vector<vector<int>> ans;
set<vector<int>> set_of_vector;
for(int m = 0; m<n-1; m++){
int y = nums[m];
for(int i= m+1;i<n-1;i++)
{
int j = i + 1;
int k = n - 1;
long x = nums[i];
while(j < k)
{
if(x + nums[j] + nums[k] + y == target)
{
vector<int> data(4);
data[0] = nums[m];
data[1] = nums[i];
data[2] = nums[j];
data[3] = nums[k];
set_of_vector.insert(data);
j++;
k--;
}
else if(x + nums[j] + nums[k] + y > target)
k--;
else
j++;
}
}
}
for(auto it = set_of_vector.begin(); it!=set_of_vector.end();it++)
{
ans.push_back(*it);
}
return ans;
}
};