-
Notifications
You must be signed in to change notification settings - Fork 0
/
15. 3sum
30 lines (29 loc) · 840 Bytes
/
15. 3sum
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
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> pi;
set<vector<int>> st;
for(int i=0;i<nums.size();i++)
{
unordered_map<int,int> mp;
for(int j=i+1;j<nums.size();j++)
{ vector<int> triplet;
int target=-(nums[i]+nums[j]);
if(mp.find(target)!=mp.end())
{
triplet.push_back(nums[i]);
triplet.push_back(nums[j]);
triplet.push_back(target);
sort(triplet.begin(), triplet.end());
st.insert(triplet);
}
mp[nums[j]]=j;
}
}
for(auto it: st)
{
pi.push_back(it);
}
return pi;
}
};