求所有因子的组合 力扣254
从小到大,分层进行dfs
class Solution { List<List<Integer>> dfs(int n, int l) { //返回的是所有因子大于l的n的因子组合 //l限制了后面加入的因子一定是大于之前,避免了重复 List<List<Integer>> ret = new ArrayList<List<Integer>>(); int i = l; while (i * i <= n) { if (n % i == 0) { ret.add(new ArrayList<Integer>(Arrays.asList(i, n / i))); //i本身 for (List<Integer> list : dfs(n / i, i)) { list.add(i); ret.add(list); } } ++i; } return ret; } public List<List<Integer>> getFactors(int n) { return dfs(n, 2); } }