-
Notifications
You must be signed in to change notification settings - Fork 4
/
3Sum.cpp
39 lines (29 loc) · 1001 Bytes
/
3Sum.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
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
const size_t len = nums.size();
if (len < 3)
return {};
std::sort(nums.begin(), nums.end());
std::set<vector<int>> unique_results;
for(int i = 0; i < len - 2; i++) {
int j = i + 1;
int k = len - 1;
while (j < k) {
const int sum = nums[i] + nums[j] + nums[k];
if (0 == sum) {
unique_results.insert({nums[i], nums[j++], nums[k--]});
}
else if (0 > sum)
j++;
else if (0 < sum)
k--;
}
}
vector<vector<int>> results;
std::copy(
unique_results.begin(), unique_results.end(),
std::back_inserter(results));
return results;
}
};