-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path191010-2.cpp
81 lines (78 loc) · 1.61 KB
/
191010-2.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// https://leetcode-cn.com/problems/3sum/
#include <cstdio>
#include <vector>
#include <set>
#include <unordered_map>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> r;
unordered_map<int, int> cnt;
set<int> cnt2;
vector<int> u;
for (auto e : nums) {
int n = ++cnt[e];
if (e != 0) {
if (n == 1) {
u.push_back(e);
} else if (n == 2) {
cnt2.insert(e);
}
}
}
auto it = cnt.find(0);
if (it != cnt.end()) {
if (it->second >= 3) {
r.push_back(vector<int>{0, 0, 0});
}
for (auto it2 = cnt.begin(); it2 != cnt.end(); ++it2) {
auto n = it2->first;
if (n < 0 && cnt.find(-n) != cnt.end()) {
r.push_back(vector<int>{n, 0, -n});
}
}
cnt.erase(it);
}
for (auto e : cnt2) {
int n = -(e + e);
if (cnt.find(n) != cnt.end()) {
r.push_back(vector<int>{e, e, n});
}
}
sort(u.begin(), u.end());
int n = u.size();
for (int i = 0; i + 1 < n; ++i) {
int a = u[i];
for (int j = i + 1; j < n; ++j) {
int b = u[j];
int c = -(a + b);
if (c > b && cnt.find(c) != cnt.end()) {
r.push_back(vector<int>{a, b, c});
}
}
}
return r;
}
};
void print(const vector<vector<int>>& r)
{
printf("[");
for (auto a : r) {
printf(" [");
for (auto e : a) {
printf(" %d", e);
}
printf(" ]");
}
printf("]\n");
}
int main() {
Solution s;
vector<int> a1{-1, 0, 1, 2, -1, -4};
print(s.threeSum(a1)); // answer: [ [-1,0,1], [-1,-1,2] ]
vector<int> a2{3,0,-2,-1,1,2};
print(s.threeSum(a2)); // answer: [ [-2,-1,3], [-1,0,1], [-2,0,2] ]
return 0;
}