Difficulty: 3
3Sum of test Instructions:
Give a column number to find out all the three numbers that satisfy the condition: three numbers add equal to 0
3Sum Closest of test instructions:
Give a column number to find out three numbers so that three numbers are added closest to target
4Sum:
Like 3sum, it's just 4 numbers.
Solution:
All the same. Basically, a couple of lines will get you through the other two.
Solution of 3Sum:
Sort by from small to large
Suppose the three numbers found A,b,c meet
A+b+c=0
A<=b<=c
Enumerate a from left to right and look for B and C in [A+1,n] to make b+c==-a
Initial order B=a+1,c=n
When b+c<-a, to increase the b+c, you should make B move right
When b+c>-a, to reduce the b+c, the C should be shifted to the left
When B+c=a, the results are recorded
O (n^2)
3Sum
Class Solution {public:vector< vector<int> > threesum (vector<int> &num) {VECTOR&L T
vector<int> > ans;
Sort (Num.begin (), Num.end ()); for (int i=0;i<num.size (); i++) {if (i-1>=0&&num[i] = = Num[i-1])//if num[i] equals num[i-1] Then the result must have been
is included {continue;
}//j,k,num[j]+num[k]=-num[i] int j=i+1;
int K=num.size ()-1;
while (J<num.size () &&k>i&&j<k) {if (num[j]+num[k]>-num[i]) k--;
else if (Num[j]+num[k]<-num[i]) j + +;
else {vector<int>tmp;
Tmp.push_back (Num[i]);
Tmp.push_back (Num[j]);
Tmp.push_back (Num[k]); if (Ans.size () >=1&&ans[ans.size () -1][0] = = Num[i]&&ans[ans.size () -1][1] = = Num[j]&& Ans[ans.size () -1][2] = = Num[k]) {j + +;
Continue
} ans.push_back (TMP);
j + +;
}}} return ans; }
};
3Sum Closest
Class Solution {Public:int threesumclosest (vector<int> &num, int target) {int ans=0x3fffffff;
Sort (Num.begin (), Num.end ()); for (int i=0;i<num.size (); i++) {if (i-1>=0&&num[i] = = Num[i-1])//if num[i] equals num[i-1] Then the result must have been
is included {continue;
}//j,k,num[i]+num[j]+num[k]=target int j=i+1;
int K=num.size ()-1;
while (J<num.size () &&k>i&&j<k) {if (num[i]+num[j]+num[k]>target)
{if (Num[i]+num[j]+num[k]-target<abs (Ans-target)) {
ANS=NUM[I]+NUM[J]+NUM[K];
} k--; } else if (Num[i]+num[j]+num[k]<target) {if (target-num[i]-num[j]-
Num[k]<abs (Ans-target)) { ANS=NUM[I]+NUM[J]+NUM[K];
} j + +;
} else {return target;
}}} return ans; }
};
4Sum
Class Solution {public:vector<vector<int> > Foursum (vector<int> &num, int target) {
Sort (Num.begin (), Num.end ());
vector<vector<int> > ans;
for (int i=0;i<num.size (); i++) {if (i>=1&&num[i] = = Num[i-1]) {
Continue
} for (int j=i+1;j<num.size (); j + +) {if (j-1>=i+1&&num[j] = = Num[j-1])
{continue;
} int k=j+1;
int L=num.size ()-1; while (K<l&&k<num.size ()) {if (Num[i]+num[j]+num[k]+num[l] > target)
l--;
else if (Num[i]+num[j]+num[k]+num[l] < target) k++; else {if (Ans.size () >0&&ans[ans.size () -1][0] = = Num[i]&&ans [Ans.size ()-1] [1] = = Num[j]& &ans[ans.size () -1][2] = = Num[k]&&ans[ans.size () -1][3] = = Num[l]) {
k++;
Continue
} vector<int>tmp;
Tmp.push_back (Num[i]);
Tmp.push_back (Num[j]);
Tmp.push_back (Num[k]);
Tmp.push_back (Num[l]);
Ans.push_back (TMP);
k++;
}}}} return ans; }
};