0 ms. C++ solution. Easy to understand


  • 0
    N
    class Solution {
    public:
        vector<vector<int> >vv;
        vector<int>v;
        void recur(int k,int target,int i,int sum)
        {
            if(v.size()==k&&sum==target)
            {
                vv.push_back(v);
                return;
            }
            if(sum>target)return;
            for(int j=i;j<10;j++)
            {
                v.push_back(j);
                recur(k,target,j+1,sum+j);
                v.pop_back();
            }
        }
        vector<vector<int>> combinationSum3(int k, int n) {
            recur(k,n,1,0);
            return vv;
        }
    };

Log in to reply
 

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