Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
combination-sum-iii.cpp 761 Bytes
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2015-07-08 13:59 +08:00 . Update combination-sum-iii.cpp
// Time: O(k * C(n, k))
// Space: O(k)
class Solution {
public:
vector<vector<int> > combinationSum3(int k, int n) {
vector<vector<int>> res;
vector<int> combination;
combinationSum3(res, combination, 1, k, n);
return res;
}
private:
void combinationSum3(vector<vector<int> > &res, vector<int> &combination, int start, int k, int n) {
if (!k && !n) {
res.push_back(combination);
return;
} else if (k < 0) {
return;
}
for (int i = start; i < 10 && n >= k * i + k * (k - 1) / 2; ++i) {
combination.push_back(i);
combinationSum3(res, combination, i + 1, k - 1, n - i);
combination.pop_back();
}
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助