Leetcode 4Sum 4 Number of sum

Source: Internet
Author: User

Test instructions: This is the 4sum after 2sum and 3sum, and similarly, to find all 4 element sequences that meet their and target. Returned by vector<vector<int>>, that is, two-dimensional, column length is 4, how many sequences there are rows, each row is unique, and ascending.

Ideas:

Method One: Using a similar 3sum method, first determine the next 1th element, and then determine the 2nd element, the remaining two elements with "two pointers." The prerequisite is that they are sorted. This method is mainly about how to weight, here are two ways to:

1) with Unordered_set, as long as a sequence is found to check if there is no such sequence, if not inserted, so that the uniqueness, and then iterate through the iteration, and then move to the vector to return.

2) This is the way I use it. Suppose there is a sequence of a{-2,-2,-2,-1,-1,0,0,1,1,2,2 } with a total of 11 elements, target=0.

The 1th element takes a[0], and the 2nd element takes a[1], then all two combinations of >1 will be found by the "two pointers", and if there are duplicates, they will be consecutive repetitions, so as long as the judgment is different from the previous sequence, it can be inserted. (Remove weight at "two pointers")

The next 2nd element will take a[2], but a[1]=a[2], is it necessary to try again? Not necessarily, see { -2,-2,a,b}=target, where A and B have already tried all possible, if this still takes a[2], then the values of A and B in the set { -2,-2,a,b} are still being tried. If you repeat it, you will not necessarily repeat it (you can list it yourself) and go back to the trouble. So the 2nd element must skip the already swept value, that is, no matter how many-2, skip directly to-1. (The de-weight at the 2nd element)

1th element to take a[1] start trying? No! The same principle as the 2nd element of the same weight. All the sequences with a 1th element of 2 have been tried out, and then 2 is just looking for a repeating sequence, and not continuous.

1 classSolution {2  Public:3vector<vector<int> > Foursum (vector<int> &num,inttarget) {4Sort (Num.begin (), Num.end ());//Sort5vector<int> Group (4,10086);6vector< vector<int> >ans; 7         intN=num.size (), Sum, sum2, *PL, *PR, old1=10086, old2=-10086;//here old can be random value, special point of the line8          for(intj=0; j<n-3; J + +)//a 1th element9         {Ten             if(Old1==num[j])Continue;Elseold1=Num[j]; One              for(inti=j+1; i<n-2; i++)//a 2nd element A             { -                 if(Old2==num[i])Continue;ElseOld2=Num[i]; -sum2 = Target-num[i]-num[j];//look for the sum of the two remaining numbers thePL = &num[i+1];//left Pointer -PR = &num[n-1];//Right Pointer -                  while(pl!=PR) -                 { +sum = *PL + *PR; -                     if(Sum = =sum2) +                     { A                         if(group[0]!=NUM[J] | | group[1]!=num[i] | | group[2]!=*PL | | group[3]!=*PR)//as long as there is a difference, you can add at                         { -group[0] =Num[j]; -group[1] =Num[i]; -group[2] = *Pl; -group[3] = *PR; - Ans.push_back (group); in                         } -pl++; to                     } +                     Else if(Sum > Sum2) pr--; -                     Elsepl++; the                 } *             } $Old2=10086;Panax Notoginseng         } -         returnans; the     } +};
4Sum

  

Method Two: The sequence of 22 and as a new sequence, the problem is converted to "two values of the", that is, the "two pointers" method. The difficulty here is to find and target two elements in a new sequence, how to find their original face (4 elements). There is another problem, the elements will be reused by you.

For example, there is a sequence a{-1,-1,0,1,1},

Turn into a new sequence {-2,-1, 0,0,-1,0,0,1,1, 2},

The new sequence is so come { -1+-1,-1+0,-1+1,-1+1,-1+0,-1+1,-1+1,0+1,0+1,1+1},

i.e. {a[0]+a[1],a[0]+a[2], a[0]+a[3],a[0]+a[4], a[1]+a[2],a[1]+a[3],a[1]+a[4],a[2]+a[3],a[2]+a[4] , A[3]+a[4]}.

If target=0, we find -1+1=0 in the new sequence, but they are a[0]+a[2]+a[2]+a[4]. That is to say, a[2] was counted two times, is not allowed.

This method has not yet been implemented and is free to think again.

Leetcode 4Sum 4 Number of sum

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.