254. Factor Combinations

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. You are assume that n are always positive.
    2. 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 topic needs to be noted is how to avoid duplication, need to add a sentinel in the backtracking inputs, in the backtracking cycle from Sentinel start.

 Publicilist<ilist<int>> Getfactors (intN) {varres =Newlist<ilist<int>>(); if(n = =0)returnRes; Backtracking (n,Newlist<int> (), Res,2); returnRes; }        Private voidBacktracking (intN, list<int> cur, ilist<ilist<int>> Res,intLast ) {        if(n==1)        {            if(cur. Count () >1) Res. ADD (Newlist<int>(cur)); }        Else        {             for(inti = last;i<= n;i++)            {                if(n%i = =0) {cur.                    ADD (i); Backtracking (n/i,cur,res,i); Cur. RemoveAt (cur. Count ()-1); }            }        }    }

The above algorithm can be optimized because the smaller multiplier does not exceed the square root of the original number when an integer is divided into two integer multiples. You can limit the upper bound of the loop to sqrt (n).

 Publicilist<ilist<int>> Getfactors (intN) {varres =Newlist<ilist<int>>(); if(n = =0)returnRes; Backtracking (n,Newlist<int> (), Res,2); returnRes; }        Private voidBacktracking (intN, list<int> cur, ilist<ilist<int>> Res,intLast ) {        if(cur. Count () >0) {cur.            ADD (n); Res. ADD (Newlist<int>(cur)); Cur. RemoveAt (cur. Count ()-1); }                     for(inti = last;i<= (int) math.sqrt (n); i++)            {                if(n%i = =0) {cur.                    ADD (i); Backtracking (n/i,cur,res,i); Cur. RemoveAt (cur. Count ()-1); }            }            }

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.