[LeetCode-interview algorithm classic-Java implementation] [015-3 Sum (Sum of three numbers)], leetcode -- java
[015-3 Sum (Sum of three numbers )][LeetCode-interview algorithm classic-Java implementation] [directory indexes for all questions]Original question
Given an array S of n integers, are there elements a, B, c in 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 be in 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)
Theme
Given an array of n elements, if there are three elements a, B, and c, use a + B + c = 0 to find all the three elements that meet this condition.
Solutions
The 3sum problem can be solved based on the 2sum problem. Assume that the target of the 3sum problem is target. Select a number k from the array each time, and find the 2sum problem where the target is equal to the target-k from the remaining number. Note that there is a small trick: When we select the I number from the array, we only need to ask the 2sum Problem of the number group from the I + 1 to the last range.
Let's take the first and second examples. Suppose the array is A [] and there are n elements in total, A1, A2 .... An. Obviously, when A1 ~ An] In the target bit target-A1 2sum problem, we want to prove that when selecting A2, we only need to in the sub array [A3 ~ The 2sum problem of the target bit target-A2 IN An], instead of in the sub array [A1, A3 ~ An.
The proof is as follows: assume that the sub-array [A1, A3 ~ In the 2sum problem of target target-A2, there exist A1 + m = target-A2 (m is A3 ~ A number in An), that is, A2 + m = target-A1, which is just "for the sub array [A3 ~ An], a solution to the 2sum problem of the target bit target-A1. That is, we calculate the three numbers A1 + A2 + m = target that meet 3sum. Therefore, to avoid repeated computation, In the subarray [A1, A3 ~ An], you can remove A1, and then calculate the target is the 2sum problem of the target-A2.
To obtain the nearest solution, you only need to save the distance between the current solution and the target. If the new solution is closer, the new solution is updated. The algorithm complexity is O (n ^ 2 );
Code Implementation
Import java. util. arrayList; import java. util. arrays; import java. util. using list; import java. util. list; public class Solution {/*** 015-3 Sum (Sum of three numbers) ** @ param nums the input array * @ return execution result */public List <Integer> threeSum (int [] nums) {List <Integer> result = new sequence List <> (); if (nums! = Null & nums. length> 2) {// sort the array first. Arrays. sort (nums); // I indicates taking the number of I as the result for (int I = 0; I <nums. length-2;) {// The possible starting position of the second number int j = I + 1; // the third number may be the ending position int k = nums. length-1; while (j <k) {// if a solution that meets the conditions is found if (nums [j] + nums [k] =-nums [I]) {// Add the result to the result set 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 position and find the next group of solutions k --; j ++; // from left to right find the subscript of the first number different from the number previously processed while (j <k & nums [j] = nums [j-1]) {j ++ ;} // find the subscript of the first number that is different from the previously processed number from the right to the left while (j <k & nums [k] = nums [k + 1]) {k -- ;}} // and greater than 0 else if (nums [j] + nums [k]>-nums [I]) {k --; // find the subscript of the first number that is different from the previously processed number from the right to the left while (j <k & nums [k] = nums [k + 1]) {k --;} // and less than 0 else {j ++; // from left to right find the subscript of the first number different from the number previously processed while (j <k & nums [j] = nums [j-1]) {j ++ ;}}// point to the next number I ++ to be processed; // from left to right find the subscript of the first number different from the previously processed number while (I <nums. length-2 & nums [I] = nums [I-1]) {I ++ ;}} return result ;}}
Evaluation Result
Click the image. If you do not release the image, drag it to a position. After the image is released, you can view the complete image in the new window.
Note Please refer to the following link for more information: http://blog.csdn.net/derrantcm/article/details/46980229]
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.