216. Combination Sum III

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.

Example 1:

Input: k= 3,n= 7

Output:

[[1,2,4]]

Example 2:

Input: k= 3,n= 9

Output:

[[1,2,6], [1,3,5], [2,3,4]]
Solution 1: DFS O(9 * 2^n); O(n)
    public List<List<Integer>> combinationSum3(int k, int n) {
        List<List<Integer>> res = new ArrayList<>();

        dfs(res, new ArrayList<>(), k, 1, n);
        return res;
    }

    private void dfs(List<List<Integer>> res, List<Integer> comb, int k, int start, int remain) {
        if (comb.size() == k && remain == 0) { //add comb.size() == k
            res.add(new ArrayList<>(comb));
        }

        for (int i = start; i <= 9; i++) {//nums.length is fixed to 9
            comb.add(i);
            dfs(res, comb, k, i + 1, remain - i); //数组固定为1到9,每个数只能用一次
            comb.remove(comb.size() - 1);
        }
    }

results matching ""

    No results matching ""