[Leetcode] [Java] 4Sum

Source: Internet
Author: User


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.


    • 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)

Test Instructions:

Given an array of n integers, there are elements a,b,c and D in the array s, making a + b + C + D= target. Find all of this combination in array S.


1. These four elements must be in ascending order(ie,a≤b≤C≤D)

2. Finally, the result is not tender, including repeated solutions.

    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)

Algorithm Analysis:

* Using the code of the title "3Sum" https://leetcode.com/problems/3sum/
* First the array is sorted in ascending order, then the first element starts by traversing the 3Sum algorithm, the target value is (Target-nums[i]), and the iterated array is the array after the element
* This satisfies the arraylist of the last element in ascending order and does not appear repeated. Because each time it starts. The elements that are traversed are the smallest elements in the array

AC Code:

public class Solution {private static arraylist<arraylist<integer>> ret = new Arraylist<arraylist<in        Teger>> ();            Public arraylist<arraylist<integer>> foursum (int[] nums, int target) {int newtarget=0;        arraylist<arraylist<integer>> finalres = new arraylist<arraylist<integer>> ();    Arrays.sort (Nums); for (int i = 0; i < nums.length; i++) {arraylist<arraylist<integer>> temlist= new arraylist&lt ;    Arraylist<integer>> ();            if (i > 0 && nums[i] = = Nums[i-1]) continue;//avoid repeated results, and it is directly skipped after it is equal to it.            Newtarget=target-nums[i];            int inputnums[]=new int[nums.length-i-1]; for (int ii=0;ii<nums.length-i-1;ii++) inputnums[ii]=nums[ii+1+i];//each time the array following the element is selected as the input array of the 3SUM algorithm T                       Emlist=threesum (Inputnums,newtarget,nums[i]);            Finalres.addall (temlist);        Ret.clear (); }return FinalreS } public static arraylist<arraylist<integer>> Threesum (int[] num,int newtarget,int firstnum) {i                F (num = = NULL | | Num.length < 3) return RET;                Arrays.sort (num);        int len = num.length; for (int i = 0; i < len-2; i++) {if (i > 0 && num[i] = = Num[i-1]) continue;//avoid repeated results, behind and            It is equal to be skipped directly. Find (num, i+1, len-1, Num[i],newtarget, firstnum);    Look for two numbers with Num[i] and for 0} return ret; public static void Find (int[] num, int begin, int end, int target,int newtarget,int firstnum) {int L        = begin, R = end; while (L < R) {if (Num[l] + num[r] + target = = Newtarget) {arraylist<                integer> ans = new arraylist<integer> (); Ans.add (firstnum);//different from the original 3Sum algorithm: Remember to add each heuristic traversal element in.                is the smallest of the 4 numbers, the Ans.add (target);                Ans.add (Num[l]); Ans.add (nuM[r]); Ret.add (ANS);                Put in the result set while (L < r && Num[l] = = num[l+1]) l++;//avoid repeated results, and it is directly skipped after it is equal to it.                while (L < r && Num[r] = = Num[r-1]) r--;////avoids repeated results, and it is directly skipped after it is equal to it.                l++;            r--;            } else if (Num[l] + num[r] + target < newtarget) l++;        else r--; }    }}

[Leetcode] [Java] 4Sum

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.