[Leetcode] Factor combinations factor combination

Source: Internet
Author: User

Numbers can regarded as product of its factors. For example,

8 = 2 x 2 x 2;  = 2 x 4.

Write a function that takes an integer n and return all possible combinations of its factors.

Note:

    1. Each combination ' s factors must are sorted ascending, for example:the factors of 2 and 6 [2, 6] are, not [6, 2] .
    2. You are assume that n are always positive.
    3. Factors should is greater than 1 and less than n.

Examples:
Input1
Output

[]

Input37
Output

[]

Input12
Output

[  [2, 6],  [2, 2, 3],  [3, 4]]

Input32
Output

[  2, +],  [2, 2, 8], [  2, 2, 2, 4],  [2, 2, 2, 2, 2],  [2, 4, 4], [4  , 8]]

This problem gives us a positive integer n, let us write all the form of multiplication of factors, and specify the factors from small to large order, then for this need to list all the cases of the problem, usually by backtracking method to solve, because the topic shows that 1 and n itself can not be counted as its factor, Then we can start from 2 to N, if the current number I can be divisible by n, indicating I is a factor of N, we put it into an array out, and then recursively call n/i, at this time do not start from 2 traversal, but from I traverse to n/i, the condition of the stop is when n equals 1 o'clock, if there is a factor in the out , we will put this combination into the result res, see the code below:

Solution One:

classSolution { Public: Vector<vector<int>> Getfactors (intN) {vector<vector<int>>Res; Helper (n,2, {}, res); returnRes; }    voidHelperintNintStart, vector<int> out, vector<vector<int>> &Res) {        if(n = =1) {            if( out. Size () >1) Res.push_back ( out); } Else {             for(inti = start; I <= N; ++i) {if(n% i = =0) {                     out. push_back (i); Helper (n/I, I, out, RES);  out. Pop_back (); }            }        }    }};

The following method uses a small trick, we carefully observe the results of the two examples given in the topic, we can find that the first number of each combination is not more than the square root of N, this is also very well understood, because the requirements sequence is from small to large arrangement, then if the first number is greater than the square root of N, and n itself is not a factor, then that factor is also bound to the square root of n, so multiply it will inevitably exceed N, so there is no such situation. So we are just beginning to traverse between 2 and N square roots, if we encounter a factor, first copy the original array out to a new one array new_out, then add this factor I to new_out, and then recursively call n/i, and from I traverse to the square root of n/i, then n/ I put into the new_out, and deposit the result res, because of the call of the layer iteration, usually can continue to split into smaller factors can be split in subsequent iterations, and add the previous results, will eventually save Res, see the code as follows:

Solution Two:

classSolution { Public: Vector<vector<int>> Getfactors (intN) {vector<vector<int>>Res; Helper (n,2, {}, res); returnRes; }    voidHelperintNintStart, vector<int> out, vector<vector<int>> &Res) {         for(inti = start; I <= sqrt (n); ++i) {if(n% i = =0) {vector<int> new_out = out;                New_out.push_back (i); Helper (n/I, I, new_out, res); New_out.push_back (n/i);            Res.push_back (new_out); }        }    }};

The above two solutions are slightly different, but the order of the constituent results is the same, for the two examples given in the topic n = 12 and n = 32, the results are as follows:

n = A2 2 32 63 4N= +2 2 2 2 22 2 2 42 2 82 4 42  -4 8

The results obtained from the above two methods are different from the answers given in the topic, although the order is different, but it does not affect its passage through OJ. We give the solution of the order in the generation of the problem, this method is not difficult to understand, or from 2 to the square root of N, if I is a factor, then we call n/i recursively, the results are saved with V, and then we create a new sequence of two factors containing I and n/i out, and then put it into the result res, Then we traverse the resulting sequence of the previous recursive n/i, if I is less than or equal to the first number of a sequence, then we insert it into the first position of the sequence, and then we put the sequence into the result res, we give an example, than n = 12, then just start I = 2, is the factor, then the 6 call recursion, get {2 3}, and then at this time {2, 6} first into the results, and then found I (at this point 2) is less than equals {2, 3} in the first digit 2, then 2 is inserted in the first position to get {2, 2, 3} Join the result, then I become 3, or factor, to 4 call recursion, get {2, 2}, this time {3, 4 Deposit the result, and then find I (at this point 3) is greater than {2, 2} in the first number 2, do not do any processing to return directly, so that we get the correct result:

Solution Three:

classSolution { Public: Vector<vector<int>> Getfactors (intN) {vector<vector<int>>Res;  for(inti =2; I * I <= N; ++i) {if(n% i = =0) {vector<vector<int>> v = getfactors (n/i); Vector<int> out{i, N/i}; Res.push_back ( out);  for(AST)a:v) {if(I <= a[0]) {A.insert (A.begin (), i);                    Res.push_back (a); }                }            }        }        returnRes; }};

For the two examples given in the topic, n = 12 and n = 32, the results are the same as those given in the topic:

n = A2 62 2 33 4N= +2  -2 2 82 2 2 42 2 2 2 22 4 44 8

Similar topics:

Combination Sum III

Combination Sum II

Combination Sum

Resources:

Https://leetcode.com/discuss/62457/concise-and-straightforward-java-solution

Https://leetcode.com/discuss/65106/share-clean-and-simple-0ms-c-solution-with-explanation

Https://leetcode.com/discuss/87331/17-lines-concise-and-easy-understand-solution-backtracking

Leetcode all in one topic summary (continuous update ...)

[Leetcode] Factor combinations factor combination

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.