LeetCode Combination Sum III
原题链接在这里:https://leetcode.com/problems/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]]
题解:
与Combination Sum II相似,不同的是中不是所有元素相加,只是k个元素相加。
所以在把item的copy 加到res前需要同时满足item.size() == k 和 target == 0两个条件. candidates里没有相同元素,所以res中不可能有duplicates. 因此没有检验去重的步骤。
Time Complexity: exponenetial.
Space: O(1). stack space最多9层.
AC Java:
class Solution {
public List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if(k<=0 || n<=0){
return res;
} dfs(k, n, 1, new ArrayList<Integer>(), res);
return res;
} private void dfs(int k, int target, int start, List<Integer> item, List<List<Integer>> res){
if(target == 0 && item.size() == k){
res.add(new ArrayList<Integer>(item));
return;
} if(target < 0 || item.size() > k){
return;
} for(int i = start; i<=9; i++){
item.add(i);
dfs(k, target-i, i+1, item, res);
item.remove(item.size()-1);
}
}
}
LeetCode Combination Sum III的更多相关文章
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- LeetCode Combination Sum III (DFS)
题意: 在1-9这9个数字中选择k个出来,若他们的和为n,则加入答案序列,注意升序. 思路: 用DFS的方式,每次决定一个数字,共决策k次.假设上个决策是第i位为5,那么i+1位的范围就是6-9. c ...
- [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 组合之和 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
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 ...
随机推荐
- 新浪微博API开放平台进行程序开发第一步(java)
申请开发者权限步骤: 1.登录sina微博,点击“应用” 2.点击“微博开发平台 我也要做开发者” 3.点击“我的应用”,填写“开发者信息” 4.点击“创建应用”,就是你将要开发的微博应用程序,可以是 ...
- Foreach 与 Foreach-Object 的区别
下面两个实例可以看出: Get-ADGroupMember -Identity "CN=gAPCHN-HGZ-IE10-Users,OU=Groups,OU=Hangzhou - Chi ...
- 连连看的设计与实现——四人小组项目(NABCD)
小组名称:天天向上 成员:王森.张政,张金生,栾骄阳 题目:连连看游戏 NABCD N(需求) 游戏最大的乐趣在于玩法,我们要想在众多的连连看游戏当中脱颖而出,就需要增加更多富有乐趣.吸引用户的玩法. ...
- [ZZ] Understanding 3D rendering step by step with 3DMark11 - BeHardware >> Graphics cards
http://www.behardware.com/art/lire/845/ --> Understanding 3D rendering step by step with 3DMark11 ...
- 【IOS笔记】Using View Controllers in Your App
参考:http://www.cnblogs.com/patientAndPersist/p/3279645.html Using View Controllers in Your App Whethe ...
- wordpress自定义数据库出错页面
wordpress数据连接出错时,会有一个空白页面,有一行字:数据连接错误.这样当然不美观,好在这个页面是可以自定义的. 在/wp-content/目录下创建'db-error.php'文件,当数据库 ...
- Oracle 语句常见错误
Merge into的注意点之ORA-30926:无法在源表中获得一组稳定的行? merge into 的内部处理是将table_source 的每一条记录和table_target的每一条记录对比匹 ...
- Nginx模块fastcgi_cache的几个注意点
fastcgi响应http请求的结果中,响应头包括Expires、Cache-Control、Set-Cookie三个,都会可能不被cache. thinkphp3.0禁止session自动启动 co ...
- 验证码 mewebstudio/captcha
composer require mews/captcha https://github.com/mewebstudio/captcha
- Delphi XE5 如何与其他版本共存
如果你想使用Delphi诸如XE4.XE3.XE2.XE之类的版本跟Delphi XE5共存的话,在cglm.ini中简单修改两行就行啦. 找到Delphi XE5的安装根目录C:\Program F ...