找出所有可能的 k 个数,使其相加之和为 n,只允许使用数字1-9,并且每一种组合中的数字是唯一的。
示例 1:
输入: k = 3, n = 7
输出:
[[1,2,4]]
示例 2:
输入: k = 3, n = 9
输出:
[[1,2,6], [1,3,5], [2,3,4]]
详见:https://leetcode.com/problems/combination-sum-iii/description/

Java实现:

class Solution {
public List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer>> res=new ArrayList<List<Integer>>();
List<Integer> out=new ArrayList<Integer>();
helper(k,n,1,out,res);
return res;
}
private void helper(int k,int n,int start,List<Integer> out,List<List<Integer>> res){
if(n<0){
return;
}
if(n==0&&out.size()==k){
res.add(new ArrayList<Integer>(out));
}
for(int i=start;i<=9;++i){
out.add(i);
helper(k,n-i,i+1,out,res);
out.remove(out.size()-1);
}
}
}

C++实现:

class Solution {
public:
vector<vector<int>> combinationSum3(int k, int n) {
vector<vector<int>> res;
vector<int> out;
helper(k,n,1,out,res);
return res;
}
void helper(int k,int n,int start,vector<int> &out,vector<vector<int>> &res)
{
if(n<0)
{
return;
}
if(n==0&&out.size()==k)
{
res.push_back(out);
}
for(int i=start;i<=9;++i)
{
out.push_back(i);
helper(k,n-i,i+1,out,res);
out.pop_back();
}
}
};

216 Combination Sum III 组合总和 III的更多相关文章

  1. 040 Combination Sum II 组合总和 II

    给定候选号码数组 (C) 和目标总和数 (T),找出 C 中候选号码总和为 T 的所有唯一组合.C 中的每个数字只能在组合中使用一次.注意:    所有数字(包括目标)都是正整数.    解决方案集不 ...

  2. 【LeetCode】Combination Sum II(组合总和 II)

    这道题是LeetCode里的第40道题. 题目要求: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. can ...

  3. Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III)

    Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III) 同类题目: Leetcode之回溯法专题-39. 组合总数(Combination Sum) Lee ...

  4. [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 ...

  5. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  6. Java实现 LeetCode 216. 组合总和 III(三)

    216. 组合总和 III 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. 解集不能包含重复的组合. ...

  7. 216. 组合总和 III

    216. 组合总和 III 题意 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. 解集不能包含重复的 ...

  8. [LeetCode] 377. Combination Sum IV 组合之和 IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  9. Leetcode 39 40 216 Combination Sum I II III

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

随机推荐

  1. Navicat for MySQL如何导入SQL文件

    1 新建一个数据库,字符集和排序规格如下   2 打开这个数据库,然后运行SQL文件即可   3 刷新一下所有表就出来了

  2. PADs 元器件PCB建库

    直接看图就好了,上图! 有几点需要记住的: 如果没有datasheet的情况下,与焊盘相比,阻焊大0.1mm,钢网小0.1mm.或者阻焊大0.05mm,钢网等大,具体要看引脚的间距. 焊盘太大,比如1 ...

  3. 全栈JavaScript之路(十六)HTML5 HTMLDocument 类型的变化

    HTML5 扩展了 HTMLDocument, 添加了新的功能. 1.document.readState = 'loading' || 'complete'  //支持readyState 属性的浏 ...

  4. 浅谈OC中的Category

    OC特有的分类Category,依赖于类.它可以在不改变原来的类内容的基础上,为类增加一些方法.分类的使用注意: (1)分类只能增加方法,不能增加成员变量: (2)在分类方法的实现中可以访问原来类中的 ...

  5. jquery源码学习笔记三:jQuery工厂剖析

    jquery源码学习笔记二:jQuery工厂 jquery源码学习笔记一:总体结构 上两篇说过,query的核心是一个jQuery工厂.其代码如下 function( window, noGlobal ...

  6. 生成器 减少代码行数 map reduce

    map reduce l = [[2, 3], [2, 3, 4]] ll = [2, 3, 4] l = [int(i) for i in set(','.join([','.join([str(i ...

  7. Mac下安装manen

    下载好maven,放到特定目录下: 打开终端: 进入根目录: cd ~ 创建文件.bash_profile: vi .bash_profile 编辑文件添加内容 MAVEN_HOME=/Users/c ...

  8. YTU 2928: 取不重复的子串。

    2928: 取不重复的子串. 时间限制: 1 Sec  内存限制: 128 MB 提交: 5  解决: 5 题目描述 输入字母构成的字符串(不大于30字符)从中读取3个不重复的字符,求所有取法,取出的 ...

  9. Persisting Data to the Device

    Persisting Data to the Device Edit PagePage History Overview The Android framework offers several op ...

  10. Mac 中安装 Apache Ant

    1.下载Apache Ant:http://ant.apache.org/bindownload.cgi 2.解压apache-ant-1.9.6-bin.zip,把解压好的apache-ant-1. ...