Leetcode 216 Solution

This article provides solution to leetcode question 216 (combination-sum-iii)

https://leetcode.com/problems/combination-sum-iii

Solution

class Solution {
    vector<vector<int>> res;

public:
    void findsum(int k, int n, vector<int>& a, int i)
    {
        if (k == 0)
        {
            if (n == 0)
                res.push_back(a);
            return;
        }

        for (int j = i; j < 10; j++)
        {
            if (j > n)
                break;

            a.push_back(j);
            findsum(k - 1, n - j, a, j + 1);
            a.pop_back();
        }
    }

    vector<vector<int>> combinationSum3(int k, int n) {
        vector<int> a;
        findsum(k, n, a, 1);
        return res;
    }
};