Leetcode 18.4Sum (4 numbers and) ideas and methods of solving problems

Source: Internet
Author: User

4Sum
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)


Idea: This problem and 3Sum is related to the problem, the problem is converted into b+c+d = Target-a =k 3Sum.

Specific methods and ideas see the following code:

    Public list<list<integer>> foursum (int[] nums, int target) {list<list<integer>> List =        New Arraylist<list<integer>> ();        int len = nums.length;        if (Len <= 3)//insufficient length, directly return to the return list; Arrays.sort (nums);//Sort//Start loop for (int i = 0; i < len-2; i++) {//if target> 0 and Nums[i]>targe T, the remaining array addition cannot be =target if (Target > 0 && nums[i] > target) | |                (Target < 0 && nums[len-1] < target))                        Break                        if (i > 0 && nums[i] = = Nums[i-1])//eliminate repetitive continue;            Converted from a+b+c+d = T to B + C + d = t-a int a = target-nums[i]; for (int j = i + 1; j < len-1;j++) {if (a > 0 && nums[j] > a) | | (A < 0 && nums[len-1] < a))                            If Nums[j]>a, then the remaining array addition cannot =a break; if (J > i+1 && nums[J] = = Nums[j-1]) continue;                int m = j+1;                                int n = len-1;                    while (M < n) {int k = Nums[j] + nums[m] + nums[n];                        if (k = = a) {list<integer> Al = new Arraylist<integer> ();                        Al.add (Nums[i]);                        Al.add (Nums[j]);                        Al.add (Nums[m]);                        Al.add (Nums[n]);                        List.add (AL);                        m++;                        n--;                        while (M < n && nums[m] = = nums[m-1]) m++;                    while (M < n && nums[n] = = nums[n+1]) n--;                        } else{//Change position mark if (K < a) m++;                    else n--;}}}} return list; }


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

Leetcode 18.4Sum (4 numbers and) ideas and methods of solving problems

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.