在3sums的基础上加了一层循环。
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
vector<vector<int>> ans;
if (nums.size() < ) return ans;
for(unsigned int j = ;j < nums.size() - ; j++)
{
if ((j>) && (nums[j]==nums[j-]))
continue;
for (unsigned int i = j+; i < nums.size()-; i++)
{
int l = i+;
int r = nums.size() - ;
while(l < r)
{
int sum = nums[i] + nums[l] + nums[r] + nums[j];
if (sum > target) r--;
else if (sum < target) l++;
else if (sum == target)
{
ans.push_back(vector<int>{nums[j], nums[i],nums[l],nums[r]});
while(l<r && nums[l] == nums[l+])l++;
while(l<r && nums[r] == nums[r-])r--;
l++;
r--;
}
}
while(nums[i] == nums[i+] && i<nums.size() -) i++;
}
}
return ans;
}
};