forked from mrigaankzoro/Hacktoberfest24
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreeSum.cpp
28 lines (28 loc) · 810 Bytes
/
ThreeSum.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
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
int target = 0;
sort(nums.begin(), nums.end());
set<vector<int>> s;
vector<vector<int>> output;
for (int i = 0; i < nums.size(); i++){
int j = i + 1;
int k = nums.size() - 1;
while (j < k) {
int sum = nums[i] + nums[j] + nums[k];
if (sum == target) {
s.insert({nums[i], nums[j], nums[k]});
j++;
k--;
} else if (sum < target) {
j++;
} else {
k--;
}
}
}
for(auto triplets : s)
output.push_back(triplets);
return output;
}
};