Lintcode Medium title: 4 Sum of four numbers

Source: Internet
Author: User
Tags lintcode

Topic

Four sum of the numbers

To an array of integers containing n, find all the four tuples (a, B, C, D) that make and are the target of the given integer in S.

Sample Example

For example, for a given array of integers s=[1, 0,-1, 0,-2, 2] and target=0. The four-tuple collection that meets the requirements is:

(-1, 0, 0, 1)

(-2,-1, 1, 2)

(-2, 0, 0, 2)

Note

Four tuples (A, B, C, D), need to meet a <= b <= c <= D

The answer cannot contain a duplicate of the four-tuple.

Solving

How to feel that the following program has been unable to modify, but in 39% test data time-out

 Public classSolution {/**     * @paramnumbers:give An array numbersbers of n integer *@paramTarget:you need to the find four elements that ' s sum of target *@return: Find all unique quadruplets in the array which gives the sum of * zero. */     PublicArraylist<arraylist<integer>> Foursum (int[] numbers,inttarget) {             //Write your code herearraylist<arraylist<integer>> result =NewArraylist<arraylist<integer>>(); if(Numbers = =NULL|| Numbers.length < 4)            returnresult;        Arrays.sort (numbers);  for(inti=0;i< numbers.length-1; i++){            if(numbers[i]==numbers[i+1])                Continue;  for(intj = numbers.length-1;j>i; j--){                if(Numbers[j] = = Numbers[j-1])                    Continue; intleft = i + 1; intright = J-1; intn = target-numbers[i]-Numbers[j];  while(left<Right ) {                     while(Numbers[left] = = Numbers[++left] && left<Right ); Left--;  while(Numbers[right] = = Numbers[--right] && left<Right ); Right++; intsum = Numbers[left] +Numbers[right]; if(n==sum) {ArrayList<Integer> Path =NewArraylist<integer>();                        Path.add (Numbers[i]);                        Path.add (Numbers[left]);                        Path.add (Numbers[right]);                        Path.add (Numbers[j]); if(!result.contains (path)) Result.add (path); }                    Else if(sum<target) { Left++; }Else{ Right--; }                }//End While}//End for}//End for        returnresult; }}
Java Code

Nine chapters above the code, the feeling and the above clearly similar, but can pass

 Public classSolution { PublicArraylist<arraylist<integer>> Foursum (int[] num,inttarget) {ArrayList<ArrayList<Integer>> rst =NewArraylist<arraylist<integer>>();        Arrays.sort (num);  for(inti = 0; i < num.length-3; i++) {            if(I! = 0 && Num[i] = = Num[i-1]) {                Continue; }             for(intj = i + 1; J < Num.length-2; J + +) {                if(J! = i + 1 && num[j] = = Num[j-1])                    Continue; intleft = j + 1; intright = Num.length-1;  while(Left <Right ) {                    intsum = Num[i] + num[j] + Num[left] +Num[right]; if(Sum <target) { Left++; } Else if(Sum >target) { Right--; } Else{ArrayList<Integer> tmp =NewArraylist<integer>();                        Tmp.add (Num[i]);                        Tmp.add (Num[j]);                        Tmp.add (Num[left]);                        Tmp.add (Num[right]);                        Rst.add (TMP); Left++; Right--;  while(Left < right && num[left] = = Num[left-1]) { left++; }                         while(Left < right && num[right] = = num[right + 1]) { Right--; }                    }                }            }        }        returnrst; }}
Java Code

In contrast to the two programs found, there are indeed errors, most importantly, when the sum==target left and right did not change, the other small error is in the reference to the online program is not changed

Changed to the following program but there was an error in the 78% test data.

 Public classSolution {/**     * @paramnumbers:give An array numbersbers of n integer *@paramTarget:you need to the find four elements that ' s sum of target *@return: Find all unique quadruplets in the array which gives the sum of * zero. */     PublicArraylist<arraylist<integer>> Foursum (int[] numbers,inttarget) {             //Write your code herearraylist<arraylist<integer>> result =NewArraylist<arraylist<integer>>(); if(Numbers = =NULL|| Numbers.length < 4)            returnresult;        Arrays.sort (numbers);  for(inti=0;i< numbers.length-4; i++){             for(intj = numbers.length-1;j>i; j--){                intleft = i + 1; intright = J-1;  while(left<Right ) {                                            intsum = Numbers[i] + numbers[j] + Numbers[left] +Numbers[right]; if(target==sum) {ArrayList<Integer> Path =NewArraylist<integer>();                        Path.add (Numbers[i]);                        Path.add (Numbers[left]);                        Path.add (Numbers[right]);                        Path.add (Numbers[j]); if(!result.contains (path)) Result.add (path); Left++; Right--; }                    Else if(sum<target) { Left++; }Else{ Right--; }                }//End While}//End for}//End for        returnresult; }}
Java Code

On the internet to see someone else program on the 14th data to go heavy, feel that this is problematic, such as:-1-1 1 1, 1-1-1 3 This is also an answer, but the above nine chapters to the program is really by the deduplication operation ....

Looking closely at the nine chapters of the procedure, found:

1,i is the first number, J is the second number, start is the third number, end is the fourth number

2. The first two numbers are de-weighed and the calculated start and end are also de-weighed, and start and end are well understood, but for the first two numbers I don't understand.

3. The above 78% error procedure changes to nine chapters, in 39% error, remove the first two go to heavy program, or in 78% error, but I found in target==92 when my program output results have 75 values and the correct answer is the same. As for why I still do not understand, the order of four numbers should be no effect, if not to go to the weight of just a few more times, but not error .... But it still has an impact.

Lintcode Medium title: 4 Sum of four numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.