找出所有可能的 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. leetcode ----Trie/stack专题

    一:Implement Trie (Prefix Tree) 题目: Implement a trie with insert, search, and startsWith methods. Not ...

  2. PHP开发者实用的代码

    一.查看邮件是否已被阅读 当你在发送邮件时,你或许很想知道该邮件是否被对方已阅读.这里有段非常有趣的代码片段能够显示对方IP地址记录阅读的实际日期和时间. <? error_reporting( ...

  3. AIX下RAC搭建 Oracle10G(五)安装oracle、建立监听

    AIX下RAC搭建系列 AIX下RAC搭建 Oracle10G(五)安装oracle.建立监听 环境 节点 节点1 节点2 小机型号 IBM P-series 630 IBM P-series 630 ...

  4. Cocos从入门到精通--《创建第一个项目:HelloWorld》

    上节课我们解说了cocos2-x v3.7版本号的下载安装,也展示了使用CocosStudio编译不同平台运行程序的方法,大家是不是对新版本号的Cocos引擎充满期待?今天我们就创建一个project ...

  5. my.cnf配置详解[转载]

    先粘贴一份mac下的mysql5.6.22的配置文件 # Example MySQL config file for medium systems. # # This is for a system ...

  6. 现在企业流行的java框架技术

    我将简短分析被用于支持这些框架的企业开发环境或工具箱,例如Borland JBuilder,Eclipse以及BEA Workbench.请记住,市场上有许多有关这些开发框架的图书;然而,在任何一篇文 ...

  7. 在win10 docker启动的centos容器中安装nginx

    我是在win10机器上搭建了一个docker,在docker启动了centos容器,在centos中安装nginx. 安装配置docker 直接在官网下载docker for windows:http ...

  8. HDU 2512 一卡通大冒险(dp)

    一卡通大冒险 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  9. YTU 2901: G-险恶逃生II

    2901: G-险恶逃生II 时间限制: 1 Sec  内存限制: 128 MB 提交: 44  解决: 14 题目描述     SOS!!!koha is trapped in the danger ...

  10. js基础用法1

    click() 对象.click() 使对象被点击.closed 对象.closed 对象窗口是否已封闭true/falseclearTimeout(对象) 清除已设置的setTimeout对象cle ...