Leetcode Note: 3Sum, leetcode note 3sum
I. Description
Ii. problem-solving skills
This question is similar to the Two Sum of another question. However, this question is used to search for three numbers in the array so that the Sum is 0. At the same time, it is required that the three numbers only appear once. If we only need to use the brute force algorithm, the time complexity is O (n ^ 3), and it is difficult to determine whether the number of this group has appeared.
If you choose to sort first, then the left and right folders are forced. Complexity: O (n2 ).
This method can be promoted to k-sum, first sort, and then do the K-2 of the cycle, in the inmost loop left and right folder forced, time complexity is: O (max (n * logn, n ^ (k-1 ))).
If you want to sort array A (number of elements is n) in ascending order, you can use the number of I in the array as the smallest of the three, you can find A combination that satisfies the sum of-A [I] From the I + 1 to n-1 numbers of A. However, if you consider this situation separately, duplicate issues may occur. Make sure there are no duplicates when I! = 0, if the number of I and the number of I-1 is the same, then do not process, directly process the I + 1 element. In this way, as long as the minimum number of three numbers increases sequentially, the solutions found by the algorithm are not repeated.
The boundary condition of this question is: Because we choose the minimum value of three numbers, so the loop for this number is [0, N-2), at the same time, to eliminate duplicate elements at the minimum value, you must start from 1st elements. If you start from 0th elements, the values 0, 0 may be ignored, therefore, the minimum elements to be duplicated must start with 1st elements.
When looking for the sum of two numbers in array A and-A [I], we can consider using array A as A sorted condition for Left and Right clamping. During left and right search, the left and right sides must be reduced to elements different from the current element. There are two reasons for this: 1. reduces the amount of computing. 2. when the sum of the two elements is exactly the same as-A [I], if the scaling operation is not performed, the number of repeated three elements may be saved, resulting in an error in the result.
Iii. Implementation Code
class Solution{public: vector<vector<int> > threeSum(vector<int> &num) { int Size = num.size(); vector<vector<int> > Result; if (Size < 3) { return Result; } sort(num.begin(), num.end()); for (int Index_outter = 0; Index_outter < (Size - 2); Index_outter++) { int First = num[Index_outter]; int Second = num[Index_outter + 1]; const int Target = 0; if ((Index_outter != 0) && (First == num[Index_outter - 1])) { continue; } int Start = Index_outter + 1; int End = Size - 1; while (Start < End) { Second = num[Start]; int Third = num[End]; int Sum = First + Second + Third; if (Sum == Target) { vector<int> Tmp; Tmp.push_back(First); Tmp.push_back(Second); Tmp.push_back(Third); Result.push_back(Tmp); Start++; End--; while (num[Start] == num[Start - 1]) { Start++; } while (num[End] == num[End + 1]) { End--; } } if (Sum < Target) { Start++; while (num[Start] == num[Start - 1]) { Start++; } } if (Sum > Target) { End--; if (num[End] == num[End + 1]) { End--; } } } } return Result; }};
Here is a solution for the Internet:
// LeetCode, 3Sum // sort first, and then force the Left and Right folders. Note that the number of duplicates is skipped, time complexity O (n ^ 2), space complexity O (1) class Solution {public: vector <int> threeSum (vector <int> & num) {vector <int> result; if (num. size () <3) return result; sort (num. begin (), num. end (); const int target = 0; auto last = num. end (); for (auto I = num. begin (); I <last-2; ++ I) {auto j = I + 1; if (I> num. begin () & * I = * (I-1) continue; auto k = last-1; while (j <k) {if (* I + * j + * k <target) {++ j; while (* j = * (j-1) & j <k) + + j;} else if (* I + * j + * k> target) {-- k; while (* k = * (k + 1) & j <k) -- k;} else {result. push_back ({* I, * j, * k}); ++ j; -- k; while (* j = * (j-1) & * k = * (k + 1) & j <k) + + j ;}} return result ;}};
Iv. Summary
In my own ideas, sort the array and select array A [1, n-1) in order) the element in serves as the median of the three numbers to find the other two numbers that meet the conditions in the sorted array. However, when selecting the median, the boundary conditions that need to be considered are complicated, and the minimum value is not selected for convenience. At the same time, after selecting the median, the sorted array is divided into two segments, in this way, the relationship between the two sides and I needs to be determined separately during the contraction, which is also prone to errors.
This is indeed a good programming technique by sorting arrays and selecting different minimum values in order to avoid repetition.