找出所有可能的 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. Android TextView设置个别字体样式

    TextView进一步深化:       Textview 能够对其文字进行格式化.       通过查询资料,了解到格式化文字的方式主要分为两大类:  第一类:HTML标签格式化文字      代码 ...

  2. 成员函数指针 C++ FAQ LITE — Frequently Asked Questions

    http://www.sunistudio.com/cppfaq/pointers-to-members.html C++ FAQ LITE — Frequently Asked Questions ...

  3. 解决SVN Cleanup时遇到错误信息:Cleanup failed to process the following paths:xxxxxxx Previous operation has not finished: run 'cleanup' if it was interrupted Please execute the 'Cleanup' command.

    解决SVN Cleanup时遇到错误信息:Cleanup failed to process the following paths:xxxxxxx Previous operation has no ...

  4. 块状元素的text-align对齐属性

    能够为块状元素(div,h1,h2,form等)内容设置位置text-align:center,left;right;

  5. 【PLSQL Developer】PLSQL Developer SQL Editor 乱码问题

    [问题]我们常常在PLSQL Developer的SQL窗体编写各种语句.当须要保存这些语句时,能够另存为文本文件,也能够复制后粘贴到Word文件里.放在Word文件里的优点是语句保留原来的格式,能够 ...

  6. 清理一下电脑垃圾,打开Eclipse发现左边的所有项目消失了

    使用360清理一下电脑垃圾,打开Eclipse发现左边的所有项目消失了,但在相应的workspace能够找到项目,这个问题已经是第三次遇到了(预计是被360清理掉Eclipse的一些部署或配置文件所导 ...

  7. 自己写的Android端HttpUtil工具类

    package com.sxt.jcjd.util; import java.io.IOException; import java.io.UnsupportedEncodingException; ...

  8. python中一切皆是对象,对象都是在堆上存放的,一切都是指针

    1 由于对象都是在堆上存放的,所以,返回值可以任意返回. 这样看来,闭包里面的外部函数的内部变量也是对象,所以,当返回的内部函数被调用时,这个外部函数的变量就没有被释放. 这样看来,返回时,不需要考虑 ...

  9. 65*24=1560<2175 对数据的统计支撑决策假设 历史数据正确的情况下,去安排今后的任务

    没有达到目标,原因不是时间投入不够,而是不用数据决策,不用数据调度定时脚本 [数据源情况统计]----># 近30天,日生效coin数目SELECT COUNT(DISTINCT coin) A ...

  10. YTU 2851: 数字游戏

    2851: 数字游戏 时间限制: 1 Sec  内存限制: 128 MB 提交: 164  解决: 85 题目描述 输入若干个正整数,将其中能写成其它两个正整数的平方和的数输出来. 例,若输入的数中有 ...