forked from UNN-ITMM-Software/advanced_cxx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbanin.cpp
30 lines (29 loc) · 1.05 KB
/
banin.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
#include <set>
#include <vector>
#include <algorithm>
class Solution {
public:
std::vector<std::vector<int>> threeSum(std::vector<int>& nums) {
using std::set;
using std::vector;
auto begin = nums.begin();
auto end = nums.end();
sort(begin, end);
set<vector<int>> ans;
// Might want to squash repetitions for nums with many repeating values
// $O(n^2)$ search * $O(\log n)$ binary search and set insertion
for(auto it = begin; it != end; ++it){
// Not iteration with :, since you can't quite cut corners as effectively
for(auto jt = it + 1; jt != end; ++jt){
int a = *it, b = *jt, target = -(a + b);
auto kt = lower_bound(jt + 1, end, target);
if(kt != end && *kt == target){
ans.insert(vector<int>{a, b, *kt}); // Modern brace initializer
}
}
}
auto abegin = ans.begin();
auto aend = ans.end();
return vector<vector<int>>(abegin, aend);
}
};