"015-3 Sum (three number and)"
" leetcode-interview algorithm classic-java Implementation" "All topic Directory Index"
Original title
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 main effect of the topic
Given an array of n elements, whether there are a,b,c three elements, use a+b+c=0 to find all the triples that meet this condition.
ideas for solving problems
The
can solve the 3sum problem on the basis of the 2sum problem, assuming that the 3sum problem is targeted at Target. Each time from the array to select a number of K, from the remaining number of the target is equal to target-k 2sum problem. One thing to note here is a small trick: when we select the number of I from the array, we only need to ask for the 2sum problem of the word group from the first i+1 to the last range in the value.
We choose the first and second examples, assuming that the array is a[] and that there are n elements in total a1,a2....an. Obviously, when we get the A1, we're looking for the 2sum problem of the target bit target-a1 in the sub array [A2~an], and we have to prove that when we are elected A2 we only need to compute the 2sum problem of the target bit a3~an in the child array [TARGET-A2], not in the child array [A1, A3~an]. The
proves as follows: Suppose there is a A1 + M = target-a2 (M is a number in A3~an) in the 2sum problem of the A1,a3~an [TARGET-A2] target bit, that is, A2 + M = target-a1, which happens to be "for a child array [A3~an] , a solution to the 2sum problem of the target bit target-a1. That is, we are repeating computations for the three-digit a1+a2+m = target that satisfies the 3sum. Therefore, in order to avoid repeated computations, in the sub array [A1,a3~an], the A1 can be removed and then the 2sum problem of the target is TARGET-A2 is computed.
for the closest solution to the subject requirement, you need to save the current solution and the distance between the current and the target, and then update the solution if the new solution is closer. The algorithm complexity is O (n^2);
Code implementation
Import java.util.ArrayList;
Import Java.util.Arrays;
Import java.util.LinkedList;
Import java.util.List; public class Solution {/** * 015-3 Sum (three number and) * * @param nums input array * @return execution Results/PU Blic list<list<integer>> threesum (int[] nums) {list<list<integer>> result = new LinkedLis
T<> ();
if (nums!= null && nums.length > 2) {//First sort of array arrays.sort (nums); I means assuming the number of I takes the result for (int i = 0; i < nums.length-2;)
{//second number possible starting position int J = i + 1;
The third number may be the end position int k = nums.length-1;
while (J < k) {//if found to satisfy the conditions of the solution if (Nums[j] + nums[k] = =-nums[i]) {
Add the result to the result contains the list<integer> List = new arraylist<> (3);
List.add (Nums[i]); List.add (Nums[j]);
List.add (Nums[k]);
Result.add (list);
Move to the next location and find the next set of k--;
j + +;
From left to right, the first subscript while (J < k && Nums[j] = = Nums[j-1]) is the number different from the number previously processed {
j + +; //Right-to-left Find the first subscript while (J < k && Nums[k] = = Nums[k +
1]) {k--;
}//and greater than 0 else if (Nums[j] + nums[k] >-nums[i]) {
k--;
From right to left, look for the first subscript while (J < k && Nums[k] = = nums[k + 1]) that is different from the number previously processed {
k--;
}//and less than 0 else { j + +;
From left to right, the first subscript while (J < k && Nums[j] = = Nums[j-1]) is the number different from the number previously processed {
j + +;
}}//point to the next number of i++ to process;
From left to right, the first subscript while (I < nums.length-2 && nums[i] = = Nums[i-1]) is the number different from the number previously processed {
i++;
}} return result;
}
}
Evaluation Results
Click on the picture, the mouse does not release, drag a position, released in a new window to view the full picture.
Special Notes Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/46980229"