[LeetCode]Factor Combinations

这个题目比较要注意的一点是如何防止重复。在dfs中,下一层的因子应该大于等于上一层的

public class Solution {
    List<List<Integer>> result = new ArrayList<List<Integer>>();
    public List<List<Integer>> getFactors(int n) {
        if (n == 1) {
            return result;
        }
        helper(n, new ArrayList<Integer>(), 2);
        return result;
    }
    public void helper(int n, List<Integer> list, int pre) {
        if (n == 1) {
            if (list.size() > 1)
                result.add(list);
            return;
        }
        for (int i = pre; i <= n; i++) {
            if (n % i == 0) {
                List<Integer> l = new ArrayList<Integer>(list);
                l.add(i);
                helper(n / i, l, i);
            }
        }
    }
}
时间: 2024-09-26 20:09:09

[LeetCode]Factor Combinations的相关文章

[LeetCode] Factor Combinations 因子组合

Numbers can be 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: Each combination's factors must be sorted ascending, for examp

LeetCode: Letter Combinations of a Phone Number [018]

[题目] Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae"

Factor Combinations

Factor Combinations Problem: Numbers can be 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: Each combination's factors must b

Leetcode:Letter Combinations of a Phone Number 手机键盘字母映射

Letter Combinations of a Phone Number: Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Outp

LeetCode:Combinations 题解

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example,If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ]DFS,递归 1 class Solution { 2 public: 3 vector<vector<int> > an

[LeetCode#254] Factor Combinations

Problem: Numbers can be 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: Each combination's factors must be sorted ascending,

254. Factor Combinations

题目: Numbers can be 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: Each combination's factors must be sorted ascending, for e

LeetCode——Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q

[Locked] Factor combinations

Numbers can be 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: Each combination‘s factors must be sorted ascending, for examp