-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4Sum.cpp
More file actions
50 lines (48 loc) · 1.63 KB
/
Copy path4Sum.cpp
File metadata and controls
50 lines (48 loc) · 1.63 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
/*
O(N*N*N)
O(N*N*N*N)
*/
class Solution {
public:
vector<vector<int> > fourSum(vector<int> &num, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
sort(num.begin(), num.end());
set<vector<int> > hset;
vector<vector<int> > result;
for(int i=0;i<num.size();i++)
{
for(int j=i+1;j<num.size();j++)
{
for(int k=j+1, l=num.size()-1;k<l;)
{
int sum=num[i]+num[j]+num[k]+num[l];
if(sum>target)
{
l--;
}
else if(sum<target)
{
k++;
}
else if(sum==target)
{
vector<int> tmp;
tmp.push_back(num[i]);
tmp.push_back(num[j]);
tmp.push_back(num[k]);
tmp.push_back(num[l]);
if(hset.find(tmp)==hset.end())
{
hset.insert(tmp);
result.push_back(tmp);
}
k++;
l--;
}
}
}
}
return result;
}
};