Leetcode 4Sum (C,c++,java,python)

Source: Internet
Author: User

Problem:

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)
Solution: With 15 3Sum very similar, but more than a heavy cycle, see: Click to open the link
To give an array, and a target integer, the array can make and for all combinations of target a,b,c,d and satisfy A<=b<=c<=d
Java source Code (527MS):
public class Solution {public list<list<integer>> foursum (int[] nums, int target) {list<list&lt        ;integer>> res=new arraylist<list<integer>> ();        int length=nums.length;        if (length<4) return res;        Arrays.sort (Nums);            for (int i=0;i<length-3;i++) {if (i>0 && nums[i]==nums[i-1]) continue;                for (int j=i+1;j<length-2;j++) {if (j>i+1 && nums[j]==nums[j-1]) continue;                int begin=j+1,end=length-1;                    while (begin<end) {int sum=nums[i]+nums[j]+nums[begin]+nums[end];                        if (sum==target) {list<integer> temp = new arraylist<integer> ();                        Temp.add (Nums[i]);                        Temp.add (Nums[j]);                        Temp.add (Nums[begin]);                        Temp.add (Nums[end]);                        Res.add (temp); begin++;end--;                        while (Begin<end && nums[begin]==nums[begin-1]) begin++;                    while (Begin<end && nums[end]==nums[end+1]) end--;                        }else if (sum>target) {end--;                    while (Begin<end && nums[end]==nums[end+1]) end--;                        }else{begin++;                    while (Begin<end && nums[begin]==nums[begin-1]) begin++;    }}}} return res; }}

C Language Source code (spents 48ms):
/** * Return An array of arrays of size *returnsize. * Note:the returned array must is malloced, assume caller calls free ().    */void QuickSort (int* nums,int start,int end) {int l=start,r=end,tmp=nums[l];    if (start>=end) return;        while (L<r) {while (L<r && nums[r]>=tmp) r--;        if (l<r) nums[l]=nums[r];        while (L<r && nums[l]<=tmp) l++;    if (l<r) nums[r]=nums[l];    } nums[l]=tmp;    QuickSort (NUMS,START,L-1);    QuickSort (Nums,l+1,end);     }int** foursum (int* nums, int numssize, int target, int* returnsize) {int** res = (int**) malloc (sizeof (int*) *1000000);    int i,j,sum,begin,end,*temp;    *returnsize=0;    if (numssize<4) return res;    QuickSort (nums,0,numssize-1);        for (i=0;i<numssize-3;i++) {if (i>0 && nums[i]==nums[i-1]) continue;            for (j=i+1;j<numssize-2;j++) {if (j>i+1 && nums[j]==nums[j-1]) continue;            begin=j+1;end=numssize-1; while (begin<End) {Sum=nums[i]+nums[j]+nums[begin]+nums[end];                    if (sum==target) {temp= (int*) malloc (sizeof (int));                    Temp[0]=nums[i];temp[1]=nums[j];temp[2]=nums[begin];temp[3]=nums[end];                    Res[*returnsize]=temp;                    (*returnsize) + +;                    begin++;end--;                    while (Begin<end && nums[begin]==nums[begin-1]) begin++;                while (Begin<end && nums[end]==nums[end+1]) end--;                    }else if (sum>target) {end--;                while (Begin<end && nums[end]==nums[end+1]) end--;                    } else{begin++;                while (Begin<end && nums[begin]==nums[begin-1]) begin++; }}}} return res;}

C + + source code (141MS):
Class Solution {public:vector<vector<int>> Foursum (vector<int>& nums, int target) {Vecto        r<vector<int>> Res;        int length=nums.size ();        if (length<4) return res;        Sort (Nums.begin (), Nums.end ());            for (int i=0;i<length-3;i++) {if (i>0 && nums[i]==nums[i-1]) continue;                for (int j=i+1;j<length-2;j++) {if (j>i+1 && nums[j]==nums[j-1]) continue;                int begin=j+1,end=length-1;                    while (begin<end) {int sum=nums[i]+nums[j]+nums[begin]+nums[end];                        if (sum==target) {vector<int> temp;                        Temp.push_back (Nums[i]);                        Temp.push_back (Nums[j]);                        Temp.push_back (Nums[begin]);                        Temp.push_back (Nums[end]);                        Res.push_back (temp);                        begin++;end--;while (Begin<end && nums[begin]==nums[begin-1]) begin++;                    while (Begin<end && nums[end]==nums[end+1]) end++;                        }else if (sum>target) {end--;                    while (Begin<end && nums[end]==nums[end+1]) end--;                        }else{begin++;                    while (Begin<end && nums[begin]==nums[begin-1]) begin++;    }}}} return res; }};

Python source code (1842MS):
Class Solution: # @param {integer[]} nums # @param {integer} target # @return {integer[][]} def foursum (self, Nums, target): Length=len (nums) res=[] If Length<4:return res nums=sorted (nums) for                I in range (length-3): If I>0 and Nums[i]==nums[i-1]:continue for J in Range (I+1,length-2):  If J>i+1 and nums[j]==nums[j-1]:continue begin=j+1;end=length-1 while begin <                        End:sum=nums[i]+nums[j]+nums[begin]+nums[end] If Sum==target: Temp=[nums[i],nums[j],nums[begin],nums[end]] Res.append (temp) begin+=1;en D-=1 while Begin<end and nums[begin]==nums[begin-1]:begin+=1 while begin&                        Lt;end and Nums[end]==nums[end+1]:end-=1 elif sum>target:end-=1 While BEGIN&LT;end and Nums[end]==nums[end+1]:end-=1 else:begin+=1 WH Ile Begin<end and Nums[begin]==nums[begin-1]:begin+=1 return res


Leetcode 4Sum (C,c++,java,python)

Related Article

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.