-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththree_sum.cpp
More file actions
38 lines (33 loc) · 1.12 KB
/
Copy paththree_sum.cpp
File metadata and controls
38 lines (33 loc) · 1.12 KB
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
#include "map";
#include "vector";
#include "algorithm";
using namespace std;
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
map <int, int> elems;
for (int i=0; i<nums.size(); i++) {
elems[nums[i]] = i;
}
vector <int> triplet;
vector <vector<int>> answer;
for (int i=0; i<nums.size()-2; i++) {
if (nums[i] > 0) {
break;
}
for (int j=i+1; j<nums.size()-1; j++) {
int numNeeded = 0 - nums[j] - nums[i];
if (elems.count(numNeeded) && elems.find(numNeeded)->second > j) {
triplet = {nums[i],nums[j],numNeeded};
answer.push_back(triplet);
}
// set second fixed element to the last occurrence in order to avoid duplicates
j = elems.find(nums[j])->second;
}
// set first fixed element to the last occurrence in order to avoid duplicates
i = elems.find(nums[i])->second;
}
return answer;
}
};