# LeetCode-15-3Sum&&4Sum

### 15. 3Sum

Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

```Note: The solution set must not contain duplicate triplets.
For example, given array S = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]```

```class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> res;
if(nums.size()<=0)
return res;
sort(nums.begin(),nums.end());
for(int i=0;i<nums.size()&&nums[i]<=0;++i){
int j=i+1,k=nums.size()-1;
while(j<k){
if(nums[i]+nums[j]+nums[k]<0)
j++;
else if(nums[i]+nums[j]+nums[k]>0)
--k;
else if(nums[i]+nums[j]+nums[k]==0){
vector<int> temp(3,0);
temp[0]=nums[i];
temp[1]=nums[j];
temp[2]=nums[k];
res.push_back(temp);
while(k>j&&nums[k]==temp[2]) //去除k的重复
k--;
while(k>j&&nums[j]==temp[1]) //去除j的重复
j++;
}
}
while(i+1<nums.size()&&nums[i+1]==nums[i])  //去除i的重复
i++;
}
return res;
}
};```

### 18. 4Sum

Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note : The solution set must not contain duplicate quadruplets.

```class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> res;
int n=nums.size();
if(n<4)
return res;
sort(nums.begin(),nums.end());
for(int i=0;i<n-3;++i){
if(i>0&&nums[i]==nums[i-1]) continue;
if(nums[i]+nums[i+1]+nums[i+2]+nums[i+3]>target) break;
if(nums[i]+nums[n-1]+nums[n-2]+nums[n-3]<target) continue;
for(int j=i+1;j<nums.size()-2;++j){
if(j>i+1&&nums[j]==nums[j-1]) continue;
if(nums[i]+nums[j]+nums[j+1]+nums[j+2]>target) break;
if(nums[i]+nums[j]+nums[n-2]+nums[n-1]<target) continue;
int begin=j+1,end=n-1;
while(begin<end){
int sum=nums[i]+nums[j]+nums[begin]+nums[end];
if(sum>target)
--end;
else if(sum<target)
++begin;
if(sum==target){
vector<int> temp(4,0);
temp[0]=nums[i];
temp[1]=nums[j];
temp[2]=nums[begin];
temp[3]=nums[end];
res.push_back(temp);
while(begin<end&&temp[2]==nums[begin])
++begin;
while(begin<end&&temp[3]==nums[end])
--end;
}
}
}
}
return res;
}
};```

70 篇文章14 人订阅

0 条评论

9140

33960

8630

30350

25580

371130

37790

1K20

41040

10130