Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.

Ensure that numbers within the set are sorted in ascending order.

就是去k个数的和加起来等于n的组合的个数,数字只能取1-9,做法比较简单就是DFS吗,这里不再赘述,我比较喜欢用private变量,这样函数的参数式写出来比较简单:

 class Solution {
public:
vector<vector<int>> combinationSum3(int k, int n){
size = k;
target = n;
vector<int> tmpVec;
tmpVec.clear();
ret.clear();
dfs(tmpVec, target, );
return ret;
}
void dfs(vector<int>& vec, int left, int start)
{
if(left < ) return;
if(left == && vec.size() == size){
ret.push_back(vec);
return;
}
for(int i = start; i <= ; ++i){
vec.push_back(i);
dfs(vec, left - i, i + );
vec.pop_back();
}
}
private:
vector<vector<int>> ret;
int target;
int size;
};

java版本的如下所示,方法仍然相同,简单的DFS:

 public class Solution {
public List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
List<Integer> tmp = new ArrayList<Integer>();
dfs(ret, tmp, 0, k, 1, n);
return ret;
} public void dfs(List<List<Integer>>ret, List<Integer>tmp, int dep, int maxDep, int start, int left){
if(left < 0)
return; //回溯
else if(left == 0){
if(dep == maxDep)
ret.add(new ArrayList<Integer>(tmp));
return;
}else{
for(int i = start; i <= 9; ++i){
tmp.add(i);
dfs(ret, tmp, dep+1, maxDep, i+1, left - i);
tmp.remove(new Integer(i));
}
}
}
}

LeetCode OJ:Combination Sum III(组合之和III)的更多相关文章

  1. [LeetCode] 377. Combination Sum IV 组合之和 IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  2. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  3. [LeetCode] 40. Combination Sum II 组合之和 II

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  4. [LeetCode] 377. Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  5. [LeetCode] 40. Combination Sum II 组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  6. [LeetCode] 216. Combination Sum III 组合之和 III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  7. LeetCode OJ:Combination Sum II (组合之和 II)

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  8. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  9. [LeetCode] Combination Sum II 组合之和之二

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  10. [Leetcode] combination sum ii 组合之和

    Given a collection of candidate numbers ( C ) and a target number ( T), find all unique combinations ...

随机推荐

  1. WebService中WSDL和WADL(转)

    转自https://blog.csdn.net/liuxiao723846/article/details/51611183#commentBox 自己加了修改批注方便自己理解. 1.Java开发We ...

  2. Python基础-文件的基本操作

    测试文件fansik内容如下:This is line 1This is line 2This is line 3This is line 4This is line 5This is line 6 ...

  3. 用django写个CMS系统

    上一篇介绍过django自带的flatpages,能够做简单的CMS.但是对于我们的真正的工作中的使用意义并不大.还是自己动手写一个吧. 不用说,一定是先从models开始的: from django ...

  4. 解决eclipse不识别Android手机的问题

    from:http://www.apkbus.com/blog-3-39569.html 搭好Android开发环境之后,用模拟器运行Android程序没有问题,但是将手机用USB连接电脑后,电脑不识 ...

  5. Harbor私有仓库搭建

    1.安装docker yum install -y dockersystemctl start dockersystemctl enable docker 2.安装docker-compose 1.下 ...

  6. PAT 天梯赛 L1-029. 是不是太胖了 【水】

    题目链接 https://www.patest.cn/contests/gplt/L1-029 AC代码 #include <iostream> #include <cstdio&g ...

  7. SQL Server 简单事务学习

    select * from bank ) ) --打开一个事务 begin tran --定义临时变量 ; set @num = @num + @@error; --上一条语句出错次数 set @nu ...

  8.  Link 

    GNU/Linux, Bash, C, PHP, Perl, JavaScript, Vim, Git http://blog.sanctum.geek.nz/about   中文數學詞典 http: ...

  9. 【HackerRank】Gem Stones

    Gem Stones John has discovered various rocks. Each rock is composed of various elements, and each el ...

  10. 请求静态文件,返回http状态码405,not allowed

    昨天在首页加了一个链接,点击这个a标签,会进入http://121.43.68.40/boxpro/template/addsite.pdf,测试环境完全没有问题,上传到正式服务器之后,点击A标签,死 ...