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 ...
随机推荐
- python模块之codecs
http://blog.csdn.net/suofiya2008/article/details/5579413
- Ubuntu SSH root user cannot login
Open /etc/ssh/sshd_config and check if PermitRootLogin is set to yes. If not, then set it to yes and ...
- 一种少见的跨目录写webshell方法
http://hi.baidu.com/kwthqquszlbhkyd/item/480716204cfa33c3a5275afa
- Union大小
在一个union类型结构中,所有的成员公用同样的存储空间,其占用的大小为其成员中需要空间最大者: union本身只保留一块地址空间,因为只有一个成员真正存储于该地址, 但这块地址也要满足内存对齐原则. ...
- 中文编码、字符集,GBK, UTF-8的概念
字符集指的是什么? 字符集是一个人为的规定,人们用一个小册子规定好"文字字符"与"数字"的对应关系. 其中,每一个字符对应的数组也称其为编码. 例如,ASCII ...
- Hibernate + proxool 连接数超过最大允许连接数
主要原因是操作完成没有释放连接,在Hibernate中增加设定 <prop key="hibernate.connection.release_mode">after_ ...
- 关于在TP的各类标签中的注意事项
name的位置上的变量是不用加$的 value位置上的变量是要加$的 <eq name="volist.id" value="$Think.post.id" ...
- 线程池ThreadPoolExecutor
线程池类为 java.util.concurrent.ThreadPoolExecutor,常用构造方法为: ThreadPoolExecutor(int corePoolSize, int maxi ...
- 蓝牙—逻辑链路控制和适配协议(L2CAP)
L2CAP(Logical Link Control and Adaption Protocol),链路控制和适配协议,位于基带层之上,将基带层的数据分组交换以便于高层应用的数据分组格式,并提供复用和 ...
- Bluetooth GATT介绍
目录 1. 介绍 2 内容 2.1 Configured Broadcast 2.2 GATT Profile Hierarchy 3 Service Interoperability Require ...