Given an array s of n integers, are there elements a, B, c into s such that A + B + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:
Elements in a triplet (A,B,C) must being in the non-descending order. (ie, a≤b≤c) 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)
The key to mastering such a problem is to do it well:
1 sort
2 The preceding two decimal places and a large number in the back are compared, if less than 0, then the front number goes forward, increases, if greater than 0, then the number of the following forward, reduce.
3 before the number and the number of the following meet, the end of this cycle.
As this procedure, the first I can be regarded as a calibration, J is the previous number, and K is the following number.
Also note: Remember to cross the number of repetitions to avoid repetition of the answer. Of course, you can use additional data results, such as set to act as a container, do not allow duplicate data, or finally use unique to dispose of duplicate elements.
It is best to use the example to walk, you can find a lot of original can't find problems. Examples that are used are best to include examples of exceptions, such as emptiness, repetition, boundary duplication, and so on.
can refer to leetcode:http://leetcode.com/2010/04/finding-all-unique-triplets-that-sums.html
Below with test complete program, convenient for beginners
#include <iostream> #include <vector> #include <algorithm> using namespace std; Class Solution {public:vector<vector<int> > Threesum (vector<int> &num) {sort (Num.begin (), Nu
M.end ());
int i=0, j=0, K=num.size ()-1;
int NUM1 = 0;
Vector<int> T (3);
Vector<vector<int> > VT;
for (; i < k-1;)
{num1 = 0-num[i];
T[0] = Num[i];
for (j = i+1, K=num.size ()-1; j < K;)
{if (Num[j] + num[k] = = NUM1) {t[1] = num[j++];
T[2] = num[k--];
Vt.push_back (t);
while (j<k && num[j] = = Num[j-1]) {j + +;
while (j<k && num[k] = = Num[k+1]) {k--;
' Else if (Num[j] + num[k] < NUM1) {j + +;
else {k--;
}} i++;
while (i<k-1 && num[i] = = Num[i-1]) {i++;
}//for (; i<k-1; i++) return VT;
}
};
int main () {vector<vector<int> > vt; int a[]= { -2,13,-5,-4,-7,8,0,-9,6,7,0,-4,2,1,-2,4};
int len = sizeof (a)/sizeof (int);
Vector<int> T (A, A+len);
Solution Solu;
VT = solu.threesum (t);
for (auto x:vt) {cout<< ";
for (auto y:x) cout<<y<< "\ T";
cout<< ")" <<endl;
} cout<<endl;
System ("pause");
return 0;
}
2014-1-24 Update
The use of the set container to prevent repetition will time out, update, look neat point, the idea is the same as above:
vector<vector<int> > Threesum (vector<int> &num)
{
vector<int> tmp (3);
vector<vector<int> > rs;
Sort (Num.begin (), Num.end ());
for (int i = 0; i < num.size (); i++)
{
for (int j = i+1, k = Num.size ()-1; j < K;)
{
if (Num[i]+num[j]+num[k] < 0) J + +;
else if (Num[i]+num[j]+num[k] >0) k--;
else
{
tmp[0] = num[i];
TMP[1] = num[j];
TMP[2] = num[k];
Rs.push_back (TMP);
J + +, k--;
while (j<k && num[j] = = Num[j-1]) j + +;
while (j<k && num[k] = = num[k+1]) k--
;
}
while (I<num.size () && num[i] = = num[i+1]) i++;
}
return RS;
}