LeetCode 216. Combination Sum III(DFS)

题目

题意:从1-9中选出k个数之和等于n,这个k个数不能有相同的,输出所有可能的k个数字的集合,结果也不能重复

题解:暴搜,从n开始,每次减去1-9中的某个数字,然后继续递归。要注意剪枝,比如1-9中的数字大于n/k的是不可能存在答案中的,如果n 的值小于sum[k]也是不会有答案的。sum[k]表示k个数字最小和的组合。当然k>=10的时候,也是没有答案的。

class Solution {
public:
    vector<vector<int>> res;
    vector<int> ans;
    map<int,int>m;
    int sum[10]={0,1,3,6,10,15,21,28,36,45};
    vector<vector<int>> combinationSum3(int k, int n) {

        if(k==0)
            return res;

        fun(n,k);

        return res;

    }

    void fun(int n,int k)
    {
        if(k==0)
        {
            if(n==0)
                res.push_back(ans);
            return;
        }

        if(n < sum[k] || k>=10)
            return;

        int i=1;
        if(ans.size()!=0)
            i = ans[ans.size()-1] + 1;
        for(;i<=9&&i<=n/k;i++)
        {
            if(m[i]!=0)
                continue;
            if(n<i)
                break;
            ans.push_back(i);m[i]=1;
            fun(n-i,k-1);
            ans.pop_back();m[i]=0;
        }
    }
};

原文地址:https://www.cnblogs.com/dacc123/p/12344825.html

时间: 2024-10-27 06:43:54

LeetCode 216. Combination Sum III(DFS)的相关文章

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

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

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

【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

[LeetCode][JavaScript]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

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

[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】Combination Sum III(middle)

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