C++ solution for Combination Sum III


  • 0
    G
    class Solution {
    private:
        vector<vector<int>> result;   
    public:
        vector<vector<int>> combinationSum3(int k, int n) {
            vector<int> combine;
            combineFun(k,n,1,combine);
            return result;
        }
        void combineFun(int k,int n, int start, vector<int> & combine){
            if(k==0){
                if(n==0)result.push_back(combine);
                return;
            }
    
            if(n<start)return;
            for(int i=start;i<10;i++){
                combine.push_back(i);
                combineFun(k-1,n-i,i+1,combine);
                combine.pop_back();
            }
        }
    };

Log in to reply
 

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