找出所有满足条件且不重复的四元组,即四元组中 a + b + c + d 的值与 target 相等
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& A, int m) {
int n=A.size(); sort(A.begin(), A.end());
set<vector<int>> ans;
unordered_map<int, vector<pair<int, int>>> mp;
for (int i=0; i<n-1; i++)
for (int j=i+1; j<n; j++) {
int s=A[i]+A[j], d=m-s;
for (auto& v : mp[d]) {
int p=v.first, q=v.second;
if (q<i) ans.insert({A[i], A[j], A[p], A[q]});
}
mp[s].push_back({i,j});
}
vector<vector<int>> t; t.assign(ans.begin(), ans.end());
return t;
}
};