0ms neat C++ solution


  • 0
    I
    void help(int k, int start, int target, vector<vector<int>>& result, vector<int>& cur) {
        if (k == 0) {
            if (target == 0) result.push_back(cur);
        } else {
            for (int i = start; i < min(target - i, 10) || (k == 1 && i < 10); i++) {
                cur.push_back(i);
                help(k - 1, i + 1, target - i, result, cur);
                cur.pop_back();
            }
        }
    }
    
    vector<vector<int>> combinationSum3(int k, int n) {
        vector<vector<int>> result;
        vector<int> buffer;
        help(k, 1, n, result, buffer);
        return result;
    }

Log in to reply
 

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