Java for LeetCode 216 Combination Sum III
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]]
解题思路:
偷懒一点,直接在Java for LeetCode 040 Combination Sum II上面再加一个depth即可,JAVA实现如下:
public List<List<Integer>> combinationSum3(int k, int n) {
int[] candidates = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
ArrayList<List<Integer>> list = new ArrayList<List<Integer>>();
if (k > 9 || n > 55 || n / k == 0)
return list;
dfs(list, candidates, 0, n, 0, k, 0);
return list;
} static List<Integer> list2 = new ArrayList<Integer>(); static void dfs(ArrayList<List<Integer>> list, int[] array, int result,
int target, int depth, int k, int depth2) {
if (result == target && depth2 == k) {
list.add(new ArrayList<Integer>(list2));
return;
} else if (depth2 >= k || result > target || depth >= array.length)
return;
for (int i = 0; i <= 1; i++) {
for (int j = 0; j < i; j++) {
list2.add(array[depth]);
depth2++;
}
dfs(list, array, result + array[depth] * i, target, depth + 1, k,
depth2);
for (int j = 0; j < i; j++) {
list2.remove(list2.size() - 1);
depth2--;
}
}
}
Java for LeetCode 216 Combination Sum III的更多相关文章
- [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 from ...
- Leetcode 216. Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- 【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】216. Combination Sum III
Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...
- LC 216. Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV
▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...
- Java实现 LeetCode 216. 组合总和 III(三)
216. 组合总和 III 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. 解集不能包含重复的组合. ...
随机推荐
- ubuntu系统下更新jdk版本
1. 添加软件源 sudo add-apt-repository ppa:webupd8team/java 2. 更新软件源 sudo apt-get update 3. 安装 jdk1.8 sudo ...
- hdu4976 A simple greedy problem. (贪心+DP)
http://acm.hdu.edu.cn/showproblem.php?pid=4976 2014 Multi-University Training Contest 10 1006 A simp ...
- BSA混合分离分析法
BSA(Bulked Segregant Analysis)又称混合群体分离分析法,是利用极端性状进行功能基因挖掘的一种方法.主要思想是将两个具有极端性状的群体进行混池测序,比较两个群体在多态位点(S ...
- POJ 3252 Round Numbers
组合数学...(每做一题都是这么艰难) Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7607 A ...
- Swift3.0P1 语法指南——集合类型
原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...
- 第2月第1天 命令(Command)模式
http://www.tracefact.net/Design-Pattern/Command.aspx 命令模式把一个请求或者操作封装到一个对象中.命令模式允许系统使用不同的请求把客户端参数化,对请 ...
- php运行出现Call to undefined function curl_init()的解决方法
解决方法如下: 1.在php.ini中找到extension=php_curl.dll,去掉前面的分号;,然后将php.ini拷贝到c:\windows. 2.重启IIS服务,或回收应用程序池即可.
- Codeforces 271 Div 2 A Keyboard
题目链接:http://codeforces.com/contest/474/problem/A 解题报告:一个矩形的键盘,上面只有规定的字符,现在按的时候总是会向某个方向按偏,也就是输入一串字符后, ...
- .net Excel乱码
.net 生成Excel乱码,如果你一直在乱码,怎么改GB2312和UTF-8也没用,那试试下面的方法吧 HttpContext.Current.Response.AppendHeader(&quo ...
- 操作PDF文件的关键技术点
一个PDF文档从大到小可以分成如下几个要素:文档.章节.小节.段落.表格.列表. com.lowagie.text.Document表示PDF文档.必须为它创建一个PDF写入器,即com.lowagi ...