254.Factor Combinations

Source: Internet
Author: User

     /** 254.Factor combinations * 2016-3-13 by Mingyang * Numbers can is regarded as product of its Factor S. For example, * 8 = 2 x 2 x 2;      8= 2 x 4. * Write a function that takes an integer n and return all possible combinations of its factors * 1. Length standard: None (fixed) * 2. Optional Range: The first is from 2 to n-1 all integers, after each progressive.      is all the integers from the last number to n-1. * 3. Take a step forward: Temp joins this number, num times this number-the online version is the number divided by I.      One less parameter, the same principle * 4. Step back: Temp Remove * 5. Special case: to the length check. */      Public StaticList<list<integer>> Getfactors (intN) {List<List<Integer>> res=NewArraylist<list<integer>>(); List<Integer> temp=NewArraylist<integer>(); DFS1 (Res,temp,1,n,2); returnRes; }
Public Static voidDFS1 (list<list<integer>> res, list<integer>temp,intNumintNintstart) { if(num==N) {Res.add (NewArraylist<integer>(temp)); return; } if(num>N)return; for(inti=start;i<n;i++){ if(n% (num*i) ==0) {//This is a clever way to judge the division of the integer, which saves a lot of unnecessary numbers
//Num=num*i, originally so separate write, but so separate write back to re-assign the initial value, the NUM changed back. So directly in the DFS1 parameters, multiplyTemp.add (i); DFS1 (Res,temp,num*i,n,i); Temp.remove (Temp.size ()-1); //num=num/i; } } }

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.