/** 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