-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4sum.cpp
More file actions
37 lines (36 loc) · 1.96 KB
/
Copy path4sum.cpp
File metadata and controls
37 lines (36 loc) · 1.96 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
class Solution {
public:
vector<vector<int> > fourSum(vector<int> &num, int target) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(num.size() < 4) return vector<vector<int> > ();
set<vector<int> > S;
vector<int> oneResult(4, 0);
sort(num.begin(), num.end());
for(int i = 0; i < num.size()-3; i++)
{
for(int j = i+1; j < num.size()-2; j++)
{
int k = j+1;
int l = num.size()-1;
while(k < l)
{
int result = num[i] + num[j] + num[k] + num[l];
if(result == target)
{
oneResult[0] = num[i];
oneResult[1] = num[j];
oneResult[2] = num[k];
oneResult[3] = num[l];
S.insert(oneResult);
k++;
l--;
}
else if(result < target) k++;
else l--;
}
}
}
vector<vector<int > > result(S.begin(), S.end());
return result;
}
};