Wednesday, October 18, 2017

216. Combination Sum III

https://leetcode.com/problems/combination-sum-iii/description/
    vector<vector<int>> combinationSum3(int k, int n) {
        vector<vector<int>> res;
        bt(k, 1, n, {}, res);
        return res;
    }
    void bt(int k, int imin, int s, vector<int> v, vector<vector<int>>& res) {
        if(s<0 || k<0) return;
        if(k==0 && s==0) {
            res.push_back(v);
            return;
        }
        for(int i=imin; i<=9; i++) {
            v.push_back(i);
            bt(k-1, i+1, s-i, v, res);
            v.pop_back();
        }
    }

No comments:

Post a Comment