-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path015.cpp
33 lines (29 loc) · 1.07 KB
/
015.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
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> answer;
if (nums.size() < 3)
return answer;
sort(nums.begin(), nums.end());
for (auto i = nums.begin(); i != nums.end() - 2; i++) {
unordered_set<int> nums_between;
// to prevent answer duplicates consider only the FIRST duplicating num
if (i == nums.begin() or *(i - 1) != *i)
for (auto j = i + 1; j != nums.end(); j++) {
// to prevent answer duplicates consider only the LAST duplicating num
if (j + 1 == nums.end() or *(j + 1) != *j) {
int num2find = -(*i + *j);
if (nums_between.find(num2find) != nums_between.end())
answer.push_back(vector<int>({*i, num2find, *j}));
}
nums_between.insert(*j);
}
}
return answer;
}
};
int main() {
return 0;
}