-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path4Sum.cpp
56 lines (51 loc) · 1015 Bytes
/
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
49
50
51
52
53
54
55
56
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<vector<int> > fourSum(vector<int> &num, int target) {
sort(num.begin(), num.end());
vector<vector<int> > retVec;
for (int i = 0; i < (int)num.size(); ++i)
{
if (i != 0 && num[i] == num[i - 1])
continue;
for (int j = i + 1; j < (int)num.size(); ++j)
{
if (j != i + 1 && num[j] == num[j - 1])
continue;
int l = j + 1;
int r = (int)num.size() - 1;
while (l < r)
{
if (l != j + 1 && num[l] == num[l - 1])
{
++l;
continue;
}
int sum = num[i] + num[j] + num[l] + num[r];
if (sum == target)
{
retVec.push_back(vector<int>());
vector<int>& v = *(retVec.rbegin());
v.push_back(num[i]);
v.push_back(num[j]);
v.push_back(num[l]);
v.push_back(num[r]);
++l;
--r;
}
else if (sum > target)
--r;
else
++l;
}
}
}
return retVec;
}
};
int main()
{
return 0;
}