forked from zhuli19901106/leetcode-zhuli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4sum_1_AC.cpp
64 lines (62 loc) · 1.77 KB
/
4sum_1_AC.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
57
58
59
60
61
62
63
64
#include <algorithm>
#include <string>
#include <unordered_set>
using std::sort;
using std::string;
using std::unordered_set;
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
unordered_set<string> hash;
vector<vector<int>> res;
vector<int> tuple(4);
int i1, i2, i3, i4;
int n = nums.size();
int sum;
string s;
sort(nums.begin(), nums.end());
for (i1 = 0; i1 < n; ++i1) {
tuple[0] = nums[i1];
for (i2 = i1 + 1; i2 < n; ++i2) {
tuple[1] = nums[i2];
i3 = i2 + 1;
i4 = n - 1;
while (i3 < i4) {
sum = nums[i1] + nums[i2] + nums[i3] + nums[i4];
if (sum < target) {
++i3;
} else if (sum > target) {
--i4;
} else {
tuple[2] = nums[i3];
tuple[3] = nums[i4];
s = sign(tuple);
if (hash.find(s) == hash.end()) {
res.push_back(tuple);
hash.insert(s);
}
do {
++i3;
} while (i3 < i4 && nums[i3 - 1] == nums[i3]);
}
}
}
}
hash.clear();
return res;
}
private:
string sign(vector<int> &v) {
int n = v.size();
if (n == 0) {
return "";
}
int i;
string s = to_string(v[0]);
for (i = 1; i < n; ++i) {
s.push_back(' ');
s += to_string(v[i]);
}
return s;
}
};