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 ...
随机推荐
- QT基本使用
安装方法: ubuntu12.04下安装QT方法:http://blog.csdn.net/xsl1990/article/details/8299756 输入以下命令: sudo apt-get i ...
- 深入理解JVM—性能监控工具
(转自:http://yhjhappy234.blog.163.com/blog/static/31632832201222691738865/) 我们知道,在JVM编译期和加载器,甚至运行期已经做了 ...
- iOS移动开发周报-第25期
iOS移动开发周报-第25期 [摘要]:本期iOS移动开发周报带来如下内容:苹果发布 iPhone6 和 Apple Watch.Swift 1.0 GM发布.Xcode 6支持PDF Vector作 ...
- Maya 学习资料
罗其胜3d角色强化 CGwhat-Maya变形金刚擎天柱建模教程 Pixar in the box - khan academy Siggraph历届优秀动画 CG软件发展史:MAYA动画十年历程 m ...
- Html - 404页面
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- Wilddog - 野狗统计
根据业务需求提出的统计代码. <!DOCTYPE HTML> <html lang="en-US"> <head> <meta chars ...
- [转]WinForm和WebForm下读取app.config web.config 中邮件配置的方法
本文转自:http://blog.csdn.net/jinbinhan/article/details/1598386 1. 在WinForm下读取 App.config中的邮件配置语句如下: Con ...
- 如何设置jquery的ajax方法为同步
jax请求默认的都是异步的如果想同步 async设置为false就可以(默认是true) var html = $.ajax({ url: "some.php", async: ...
- switch parser.p4源码
/* Copyright 2013-present Barefoot Networks, Inc. Licensed under the Apache License, Version 2.0 (th ...
- PHP 开发 APP 接口 学习笔记与总结 - XML 方式封装通信接口
1.PHP 生成 XML 数据 ① 拼接字符串 ② 使用系统类(DomDocument,XMLWriter,SimpleXML) 例1 使用 PHP 系统类中的 DomDocument 类: < ...