Posted on

Description

Submission

class Solution {
public:
    vector<vector<int>> combinationSum3(int k, int n) {
        vector<bool> used(10, false);
        vector<int> hand;
        vector<vector<int>> res;
        backtrack(k, n, 1, hand, res);
        return res;
    }
    
private:
    void backtrack(int k, int remaining, 
                   int startIndex, vector<int>& hand, vector<vector<int>>& res) {
        if(remaining < 0) return;
        if(k == 0) {
            if(remaining == 0) res.push_back(hand);
            return; 
        } 
        
        for(int i = startIndex; i < 10; ++i) {
            hand.push_back(i);
            backtrack(k - 1, remaining - i, i + 1, hand, res);
            hand.pop_back();
        }
        
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *