A 0 ms C++ solution.


  • 1
    void helper(int k, int ta, int start, vector<vector<int>>& ret, vector<int>& path)
    {
    	if (!k)
    	{
    		if (!ta)
    			ret.push_back(path);
    		return;
    	}
    	int i = start;
    	for (i; ta >= i && i < 10; ++i)
    	{
    		path.push_back(i);
    		helper(k - 1, ta - i, i + 1, ret, path);
    		path.pop_back();
    	}
    	return;
    }
    vector<vector<int>> combinationSum3(int k, int ta) 
    {
    	vector<vector<int>> ret;
    	if (k > 9 || k < 1 || ta < 1 || ta > 45)
    		return ret;
    	vector<int> path;
    	helper(k, ta, 1, ret, path);
    	return ret;
    }

  • 0
    X

    really nice solution


  • 0
    I
    This post is deleted!

Log in to reply
 

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