本文主要是介绍leetcode 216. 组合总和 III c++ 2021-07-10,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
class Solution {
public:
vector<vector<int>> combinationSum3(int k, int n)
{
backTracing(k, n, 0, 1);
return result;
}
private:
vector<vector<int>> result;
vector<int> path;
void backTracing(int k, int n, int sum, int startIndex);
};
inline void Solution::backTracing(int k, int n, int sum, int startIndex)
{
if (sum > n)//减少循环都是剪枝
{
return;
}
if (path.size() == k)
{
if (sum == n) result.push_back(path);
return;
}
for (int i = startIndex; i <= (9 - (k - path.size()) + 1); ++i)
{
path.push_back(i);
sum += i;
backTracing(k, n, sum, i + 1);
sum -= i;
path.pop_back();
}
}
这篇关于leetcode 216. 组合总和 III c++ 2021-07-10的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!