-
Notifications
You must be signed in to change notification settings - Fork 33
/
3-sum(AC).cpp
51 lines (48 loc) · 1.37 KB
/
3-sum(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
#include <algorithm>
#include <string>
#include <unordered_set>
using namespace std;
class Solution {
public:
/**
* @param numbers : Give an array numbers of n integer
* @return : Find all unique triplets in the array which gives the sum of zero.
*/
vector<vector<int> > threeSum(vector<int> &nums) {
vector<int> &a = nums;
vector<int> v(3);
int n = a.size();
ans.clear();
int i, j, k;
sort(a.begin(), a.end());
for (i = 0; i < n - 2; ++i) {
j = i + 1;
k = n - 1;
while (j < k) {
if (a[i] + a[j] + a[k] < 0) {
++j;
} else if (a[i] + a[j] + a[k] > 0) {
--k;
} else {
string s = calcSign(a[i], a[j], a[k]);
if (us.find(s) == us.end()) {
v[0] = a[i];
v[1] = a[j];
v[2] = a[k];
ans.push_back(v);
us.insert(s);
}
++j;
}
}
}
us.clear();
return ans;
}
private:
unordered_set<string> us;
vector<vector<int> > ans;
string calcSign(int a, int b, int c) {
return to_string(a) + to_string(b) + to_string(c);
}
};