216 Combination Sum III

public class Combination_Sum_III {

    /**
     * 思路:
     *  只能使用1~9
     * 例如:
     *  给定n=9 k=3,说明用3个数组成的和要等于9
     * 当sum不等于9时,一直往后走:
     *      [123]!=9 ==> [124]!=9 ==> [125]!=9
     * 当sum等于9时,返回找下一个可能
     *      [126]=9  (原因:再往后走,就会比9大了,因为是递增的数列)
     *  ==> [134]...
     * ---------------数据结构----------------
     * 那么就需要一个数据结构保存1~9以及保存当前visit过哪些数
     * 数组和下标(因为数组是递增的,所以一个下标可以表示,下标左边的就是visit过的,右边就是没有visit的
     * ---------------代码思路----------------
     * 每一次传target,可以传剩余的target,到返回条件的时候,target就为0
     *
     */
    public static List<List<Integer>> combinationSum3(int k, int n) {
        List<List<Integer>> result = new ArrayList<>();
        int[] arr = {1,2,3,4,5,6,7,8,9};
        helper(result, new ArrayList<>(), arr, k, n, 0);
        return result;
    }

    /**
     *
     * @param result 返回的结果
     * @param list 每一条路径
     * @param arr 标尺[1,2,3,4,5,6,7,8,9]
     * @param k 给定的多少个数
     * @param target 给定的目标sum
     * @param index 当前的下标
     */
    private static void helper(List<List<Integer>> result, ArrayList<Integer> list, int[] arr, int k, int target, int index) {
        if (target == 0 && k == 0) { // 正确结果的condition
            result.add(new ArrayList<>(list));
            return;
        }
        if (target < 0 || k < 0 || index > arr.length) { //非正常结束的情况
            return;
        }
        for (int i = index; i < arr.length; i++) {
            list.add(arr[i]);
            helper(result, list, arr, k - 1, target - arr[i], i + 1);
            list.remove(list.size() - 1);
        }
    }
}
时间: 2024-10-26 07:02:08

216 Combination Sum III的相关文章

【LeetCode】216. Combination Sum III

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. Ensure that numbers within the set are sorted in ascend

Java for LeetCode 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. Ensure that numbers within the set are sorted in ascending order. Example 1

Leetcode 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

[leedcode 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. Ensure that numbers within the set are sorted in ascending order. Example 1

leetcode 216. Combination Sum III 求和III ---------- java

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

216. Combination Sum III java solutions

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

LeetCode 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

216. Combination Sum III——本质DFS

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

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