class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int> >answer;
set<vector<int> >ans;
int n=nums.size();
if(n<4){
return answer;
}
sort(nums.begin(),nums.end());
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
int sum=nums[i]+nums[j];
int flag=target-sum;
int begin=j+1,end=n-1;
while(begin<end){
if(nums[begin]+nums[end]==flag){
vector<int>Q;
Q.push_back(nums[i]);
Q.push_back(nums[j]);
Q.push_back(nums[begin]);
Q.push_back(nums[end]);
ans.insert(Q);
begin++;
end--;
}
else if(nums[begin]+nums[end]>flag){
end--;
}
else{
begin++;
}
}
}
}
set<vector<int> >::iterator iter=ans.begin();
for(;iter!=ans.end();iter++){
answer.push_back(*iter);
}
return answer;
}
};