LeetCode之Combination Sum III

2023-12-07 13 0

/*深度搜索。采用深度搜索,每次添加一个数字。*/
class Solution {
public:vector<vector<int>> combinationSum3(int k, int n) {vector<vector<int> > res;if(k <= 0 || n < (k+1)*k/2) return res;vector<int> path;for(int i = 1; i < 10; ++i) combinationSum3(res, path, i, n, k);return res;}void combinationSum3(vector<vector<int> > &res, vector<int> &path, int cur, int sum, int k){path.push_back(cur);sum -= cur;if(path.size() == k){if(sum == 0) res.push_back(path);path.pop_back();sum += cur;return;}for(int i = cur + 1; i < 10; ++i) combinationSum3(res, path, i, sum, k);path.pop_back();sum += cur;}
};

代码编程
赞赏

相关文章

wangEditor初体验
几个Python小技巧
python 实现简单的进度条 功能。
辣子鸡丁的做法
count 不统计null数量
基于HTTP/2的REST API的好处