216. Combination Sum III**
来源:互联网 发布:无人机数据算法 编辑:程序博客网 时间:2024/04/29 15:46
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.
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]]
public class Solution { public List<List<Integer>> combinationSum3(int k, int n) { List<List<Integer>> list = new ArrayList<List<Integer>>(); backtrack(list, new ArrayList<Integer>(), k, 1, n); return list; } private void backtrack(List<List<Integer>> list, List<Integer> tempList, int num, int start, int remain){ if(remain==0&&num==0) list.add(new ArrayList(tempList)); if(start>remain||num<0) return; for(int i=start;i<=9;i++){ tempList.add(i); backtrack(list,tempList,num-1,i+1,remain-i); tempList.remove(tempList.size()-1); } }}总结:你太棒了,看来还是已经掌握了这种题目的思路。你好棒。
0 0
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 216. Combination Sum III
- 51单片机寄存器表
- 07-Windows Server 2012 R2 会话远程桌面-标准部署-RD授权
- r 直接从网上获取数据
- Spark MLlib实现的中文文本分类–Naive Bayes
- android中选择文件,部分手机找不到文件路径问题的解决
- 216. Combination Sum III**
- 小程序图片上传
- jQuery给input绑定回车事件
- CSS3之复杂选择器、内容生产、多列、CSS Hack
- hosts文件被删除可以怎么办!
- uva 1395 Slim Span (最小生成树||(最大边减最小边尽量小))
- 06-Windows Server 2012 R2 会话远程桌面-标准部署-RD网关(RemoteApp)
- 【jzoj3848】【大水题】【数位动态规划】
- Linux增加分区大小