【answer】
First n digital number in ascending order.
Then enumerate triples the left-most digit of the i-th digit.
After two pointers l, r acquires moving the second and third triplets of digits.
(Initial value, l = i + 1, r = n-1);
If a [i] + a [l ] + a [r]> 0
then shows two numbers behind a [l] and a [r] is too big.
Which was so large that number a [r] becomes smaller.
That r--
otherwise l ++ can be.
This gives us to find two numbers in a one-dimensional array and provides a guideline for the number of tuple x.
And even if l = 1, r = n
If a [l] + a [r ]> x , then, let r--.
Otherwise, let ++ L.
(Of course, the premise is that you are an ordered array. So the complexity depends on the sort of velocity> _ <(moving two pointers complexity is O (N))) is
[Code]
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int> > ans;
vector<int> temp;
temp.resize(3);
sort(nums.begin(),nums.end());
int len = nums.size();
for (int i = 0;i < len;i++){
if (i>0 && nums[i]==nums[i-1]) continue;//start 相同
int l = i+1,r = len-1;
while (l<r){
if (nums[i]+nums[l]+nums[r]==0){
temp[0] = nums[i],temp[1] = nums[l],temp[2] = nums[r];
ans.push_back(temp);
while (l+1<len && nums[l+1]==nums[l]) l++;
while (r-1>i && nums[r-1]==nums[r]) r--;
l++;r--;
}else if (nums[i]+nums[l]+nums[r]>0){
r--;
}else {
l++;
}
}
}
return ans;
}
};