Combination Sum III —— LeetCode
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.
Example 1:
Input: k = 3, n = 7
Output:
[[1,2,4]]
Example 2:
Input: k = 3, n = 9
Output:
[[1,2,6], [1,3,5], [2,3,4]]
题目大意:从1~9找出所有的k个数的组合,使其和等于n。
解题思路:还是回溯,每个数只能出现一次,每次递归都从当前数的下一个开始,当k==0&&n==0,就可以加入结果集。
public List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer>> res = new ArrayList<>();
Deque<Integer> tmp = new ArrayDeque<>();
if (n == 0 || k == 0 || n / k > 9) {
return res;
}
helper(res, tmp, 1, k, n);
return res;
}
private void helper(List<List<Integer>> res, Deque<Integer> tmp, int start, int k, int n) {
if (0 == n && k == 0) {
res.add(new ArrayList<>(tmp));
// System.out.println(tmp);
return;
}
for (int i = start; i <= 9; i++) {
tmp.addLast(i);
helper(res, tmp, i + 1, k - 1, n - i);
tmp.removeLast();
}
}
Combination Sum III —— LeetCode的更多相关文章
- Combination Sum III - LeetCode
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [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 ...
- [Leetcode 216]求给定和的数集合 Combination Sum III
[题目] Find all possible combinations of k numbers that add up to a number n, given that only numbers ...
- Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III)
Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III) 同类题目: Leetcode之回溯法专题-39. 组合总数(Combination Sum) Lee ...
- 【LeetCode】216. Combination Sum III
Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- Leetcode题解(4):L216/Combination Sum III
L216: Combination Sum III Find all possible combinations of k numbers that add up to a number n, giv ...
- 【刷题-LeetCode】216. Combination Sum III
Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...
- Combination Sum,Combination Sum II,Combination Sum III
39. Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique co ...
随机推荐
- weex APIs
1.通过这个$vm()上下文访问这些api在脚本的方法 <script> module.exports = { methods: { somemethod: function() { th ...
- python爬虫scrapy的Selectors参考文档
http://doc.scrapy.org/en/1.0/topics/selectors.html#topics-selectors-htmlcode
- 脚本动态监控input
Jquery $('input').bind('input propertychange', function() { //进行相关操作 }); JS if(isIE) { document.getE ...
- java中的类集框架
1.什么是类集框架 1.是一组类和接口 2.位于java.util包当中 3.主要用于用户存储和管理对象 4.主要分为三大类——集合.列表和映射 2.类集框架图 虚线框的表示接口,实线框的表示实现类 ...
- vs2013下的WCFRest 模板开发WCF
在vs2013下使用wcfRestservice40 是安装不成功的,尝试多遍,都是这样.查看以前vs2012做的wcfrest,经过调教,终于在vs2013下也可以了! 1.新建wcf服务应用程序 ...
- tomcat的webapp下的root文件夹的作用是什么
1.基本一样..只是表示不同的tomcat的http路径而已. root目录默认放的是tomcat自己的一个项目,如:http://localhost:8080/默认访问root项目 对于webapp ...
- XCODE6 提交至 App Store
新到一个公司,以前的苹果开发人员离职,临时接手他的苹果代码,需要修改并上线到APP STORE. xcode6.0升级到最新的6.1后, 发现各种坑 1. 路径配置不对, 这个是个人习惯问题,之前的 ...
- 【转】iOS-Core-Animation-Advanced-Techniques(五)
原文:http://www.cocoachina.com/ios/20150105/10829.html 图层时间和缓冲 图层时间 时间和空间最大的区别在于,时间不能被复用 -- 弗斯特梅里克 在上面 ...
- Wpf自定义路由事件
创建自定义路由事件大体可以分为三个步骤: ①声明并注册路由事件. ②为路由事件添加CLR事件包装. ③创建可以激发路由事件的方法. 以ButtonBase类中代码为例展示这3个步骤: public a ...
- CSS居中的方法总结
[水平居中] 行内:text-align:center; 定宽块状:1.left:0 right:0然后用margin: auto外边距填充,水平方向不会发生外边距叠加; 2.绝对定位(父元素定位不 ...