Simple C++ solution


  • 0
    M
    class Solution {
    public:
        vector<vector<int>> combinationSum3(int k, int n) {
            return f(k,n,10);
        }
        vector<vector<int>> f(int k, int n, int l){
            vector<vector<int>> output;
            if (k <= 0 || n <= 0){
                if (k == 0 && n == 0) 
                    output.push_back({});
                return output;
            }
            for (int i = 1; i < l; ++i){
                for (auto a : f(k-1,n-i,i)){
                    a.push_back(i);    
                    output.push_back(a);
                }
            }
            return output;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.