"Leetcode-Interview algorithm classic-java Implementation" "018-4sum (four numbers and)"

Source: Internet
Author: User

"018-4sum (four-digit and)" "leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index" Original Question

Given an array S of n integers, is there elements a, B, C, and D in S such that A + B + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
  Note:
Elements in a quadruplet (a,b,c,d) must is in non-descending order. (ie, a≤b≤c≤d)
The solution set must not contain duplicate quadruplets.

 For example, given array S = {1 0 -1 0 -2 2}, and target = 0. A solution set is: (-1, 0, 0, 1) (-2, -1, 1, 2) (-2, 0, 0, 2)

Main Topic

Given an array of integers, find the unique solution for A + B + c + d = target.

Thinking of solving problems

First determine the two numbers of a and d, for a and D two numbers, cannot be reused at the same time. Then determine B and C, and the same two numbers cannot be reused at the same time. Find all the solutions that satisfy the condition, and guarantee that the solution will not be duplicated.

Code Implementation
Import Java.util.arraylist;import java.util.arrays;import java.util.linkedlist;import java.util.List; Public classSolution { PublicList<list<integer>> foursum (int[] num, int target) {list<list<integer>> result =NewLinkedlist<> ();if(num = =NULL|| Num.length <4) {returnResult } arrays.sort (num);//Sort an array         for(int i =0; I < num.length-3; i++) {//First addend            if(I >0&& Num[i] = = Num[i-1]) {//First addend use non-repeatingContinue } for(Int j = num.length-1; J > i +2; j--) {//Fourth a addend                if(J < Num.length-1&& Num[j] = = Num[j +1]) {//Fourth Addend use non-repeatingContinue } int start = i +1;//A second addendIntEnd= J-1;//A third addendint n = target-num[i]-num[j]; while(Start <End) {if(Num[start] + num[End] = = N) {list<integer> four =NewArraylist<> (4);                        Four.add (Num[i]);                        Four.add (Num[start]); Four.add (num[End]);                        Four.add (Num[j]); Result.add (four); Do{start++; } while(start<End&& Num[start] = = Num[start-1]);//Guaranteed re-use second number not duplicated                         Do{End--; } while(Start <End&& num[End] = = num[End+1]);//Guaranteed re-use third number not duplicated}Else if(Num[start] + num[End] < N) { Do{start++; } while(start<End&& Num[start] = = Num[start-1]);//Guaranteed re-use second number not duplicated}Else{ Do{End--; } while(Start <End&& num[End] = = num[End+1]);//Guaranteed re-use third number not duplicated}                }            }        }returnResult }}
Evaluation Results

  Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.

Special Instructions Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/46997233"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Leetcode-Interview algorithm classic-java Implementation" "018-4sum (four numbers and)"

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.