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 ...
随机推荐
- vim 配置 设置搜索 高亮
1.可以修改配置文件在~/.vimrc 文件中添加一行 set hlsearch 2.也可以直接在当前编辑环境里使用:在当前环境中执行如下命令 :set hlsearch 作者:风波 mail:fen ...
- ITIL图示
- ElasticSearch实战-日志监控平台
1.概述 在项目业务倍增的情况下,查询效率受到影响,这里我们经过讨论,引进了分布式搜索套件——ElasticSearch,通过分布式搜索来解决当下业务上存在的问题.下面给大家列出今天分析的目录: El ...
- JSValidation客户端验证框架
下载 引入文件 <script src="/Example4/js/validation-framework.js"></script> <!DOCT ...
- CSS兼容性(IE和Firefox)技巧大全
CSS对浏览器的兼容性有时让人很头疼,或许当你了解当中的技巧跟原理,就会觉得也不是难事,从网上收集了IE7,6与Fireofx的兼容性处理技巧并整理了一下.对于web2.0的过度,请尽量用xhtml格 ...
- poj 2186 tarjan求强连通分量
蕾姐讲过的例题..玩了两天后才想起来做 貌似省赛之后确实变得好懒了...再努力两天就可以去北京玩了! 顺便借这个题记录一下求强连通分量的算法 1 只需要一次dfs 依靠stack来实现的tarjan算 ...
- NV Maxwell architecture
按照NVIDIA的路线图来看,GTX 600以及GTX 700系列所采用的Kepler架构已经垂垂老矣,最早在明年第一季度,其继任者Maxwell架构可能就会和我们正式见面了.目前外媒已经放出了关于M ...
- VIM常用快捷键~网页上查找
转自~木枫林 转自~鸟哥的私房菜 第十章.vim 程序编辑器 第十章.vim 程序编辑器 最近更新日期:2009/08/20 2. vi 的使用 2.1 简易执行范例 2.2 按键说明 2.3 一个案 ...
- 用宏 x y z,找出最大值最小值
#define max(x,y,z) ((x)>(y)?(x):(y))>(z)?((x)>(y)?(x):(y)):(z) #define min(x,y,z) ((x)<( ...
- 解决ORA-00054资源正忙的问题
有时候在drop表或者其他对象的时候,会遇到ORA-00054:资源正忙,要求指定NOWAIT(中文字符集)或者ORA-00054: resource busy and acquire with NO ...