Simple , recursive only solution 0ms


  • 0
    L
        vector<vector<int>> res;
    void combsum( int target, vector<int> &cur, int k, int i=0)
    {
        if(  k<=0)
        {
            if(target == 0)
            res.push_back(cur);
            return;
        }
        
        
        if( i==10 || target <=0)
            return;
        
        if(target - i >= 0)
        {
            cur.push_back(i);
            combsum( target-i, cur, k-1, i+1);
            cur.pop_back();
        }
        combsum(target,cur,k, i+1);
    
    }
    vector<vector<int>> combinationSum3(int k, int n) {
        vector<int> cur;
        if(n>=k)
        combsum(n, cur, k, 1);
        return res;
    }

Log in to reply
 

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