[leetcode#254] Factor Combinations

Source: Internet
Author: User
Tags chop

problem:

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]]

Analysis:

This problem isn't hard , could was easily solved through DFS method. However, I came up with a very complicated method initially. Initial Idea:each factor must be consisted through multi prime numbers, thus we could dissect theNInto a collection of prime numbers. Step1:compute the collection of prime numbers forN. (which must be unique). Step2: Use the elements on the collection to generate various combinations. The above idea was right, but it was too complex. According to problem, asLongAs the factor is not 1, we could include it. Thus, we not we Do  This?considering the process of dissecting a number: (((2) * 3) * 5) * 6 = 180Suppose our target isWe can first chop out factor 2. Then our target becomes 90. Then we canContinueThe process, until our target Reachs 1. (which means we fully factorize it). Since ThisProblem asks us to compute all such combinations, we should alsoTryTo chop out factor "(((2) * 3)" "(((2) * 3) * 5)". This process could is elegantly achieved through:<at present, we is in "helper (ret, item, N, i)" > for(inti = start; I <= N; i++) {    if(n% i = = 0) {... helper (ret, item, n/I, i); ...    }} So elegantly, right?A. for(inti = start; I <= N; i++), searches through all possible numbers through start-to-n.note:you may ask since"1" is not a allowed in the combination, so we allow I <= N. Even n*1 is allowed, and in the recursive process, it no longer the N as the initial one. 2 (current N) of 4 (inital N), apparently we-should allow I-be n, otherwise, we would never reach the base Case.-------------------------------------------------------if(n = = 1) {    if(Item.size () > 1) {Ret.add (NewArraylist<integer>(item)); }    return;}-------------------------------------------------------Note the Caseof "N * 1" would never include in to the RET set, since we require "item.size ()" must larger than 1. So elegant and smart? Right!!!!Also Take advantage of item' s size!!!! Great programming skill!B. (N% i = = 0), guarantees the current I was a factor. C. Helper (ret, item, n/I, I), makes us toContinueThe search with updated target "n/i". Note:since the same factor could appears repeatedlly, we should start from"I" rather than "i+1".

Solution:

 Public classSolution { PublicList<list<integer>> Getfactors (intN) {List<List<Integer>> ret =NewArraylist<list<integer>> (); Helper (ret,NewArraylist<integer> (), N, 2); returnret; }        Private voidHelper (list<list<integer>> ret, list<integer> item,intNintstart) {        if(n = = 1) {            if(Item.size () > 1) {Ret.add (NewArraylist<integer>(item)); }            return; }         for(inti = start; I <= N; i++) {            if(n% i = = 0) {item.add (i); Helper (ret, item, n/I, i); Item.remove (Item.size ()-1); }        }    }}

[leetcode#254] Factor combinations

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.