###### 18. 4Sum

class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
int n=nums.size();
int i,j,k,t;
sort(nums.begin(),nums.end());

vector<vector<int>> res;
for(i=0;i<=n-4;i++)
{
if(i>0&&nums[i]==nums[i-1])
continue;
for(t=n-1;t>=i+3;t--)
{
if(t<n-1&&nums[t]==nums[t+1])
continue;
for(j=i+1,k=t-1;j<k;)
{
if(j>i+1&&nums[j]==nums[j-1])
{
j++;
continue;
}
if(k<t-1&&nums[k]==nums[k+1])
{
k--;
continue;
}
int d=nums[i]+nums[t]+nums[j]+nums[k];
if(d==target)
{
vector<int> vt;
vt.push_back(nums[i]);
vt.push_back(nums[j]);
vt.push_back(nums[k]);
vt.push_back(nums[t]);
res.push_back(vt);
j++;
k--;
}
else if(d>target)
k--;
else
j++;
}
}
}
return res;
}
};

#### LeetCode 18 4Sum (C,C++,Java,Python)

2015-05-10 15:04:02

#### 18. 4Sum Leetcode Python

2015-01-17 11:55:43

#### (Java)LeetCode-18. 4Sum

2016-05-30 19:59:46

#### leetcode 18 -- 4Sum

2015-06-04 15:12:14

#### leetcode-Java-18. 4Sum

2016-06-03 18:18:09

#### 18.4Sum

2015-04-04 09:53:55

#### 18. 4Sum (python)

2016-10-30 16:16:56

#### leetcode 18. 4Sum KSum的解决办法

2017-08-31 15:50:38

#### leetcode解题之 18. 4Sum Java版（结果是目标值的四个数字和）

2017-04-06 18:43:11

#### [leetcode: Python]18.4Sum

2017-05-19 08:18:55

18. 4Sum