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.

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]]

Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.

求给定的k个数字的和等于数字n的所以组合。也就是数字的个数固定了为k,使得它们的和等于n的组合。

Java:

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

Python:

class Solution:
# @param {integer} k
# @param {integer} n
# @return {integer[][]}
def combinationSum3(self, k, n):
result = []
self.combinationSumRecu(result, [], 1, k, n)
return result def combinationSumRecu(self, result, intermediate, start, k, target):
if k == 0 and target == 0:
result.append(list(intermediate))
elif k < 0:
return
while start < 10 and start * k + k * (k - 1) / 2 <= target:
intermediate.append(start)
self.combinationSumRecu(result, intermediate, start + 1, k - 1, target - start)
intermediate.pop()
start += 1  

C++:

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

类似题目:

[LeetCode] 77. Combinations 全组合

[LeetCode] 39. Combination Sum 组合之和

[LeetCode] 40. Combination Sum II

[LeetCode] 216. Combination Sum III

[LeetCode] 377. Combination Sum IV

All LeetCode Questions List 题目汇总

[LeetCode] 216. Combination Sum III 组合之和 III的更多相关文章

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

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

  2. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  3. [LeetCode] 40. Combination Sum II 组合之和 II

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

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

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

  5. [LeetCode] 40. Combination Sum II 组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  6. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  7. [LeetCode] Combination Sum IV 组合之和之四

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

  8. [LeetCode] Combination Sum II 组合之和之二

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  9. LeetCode OJ:Combination Sum II (组合之和 II)

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

随机推荐

  1. js 预解析以及变量的提升

    js在执行之前会进行预解析. 什么叫预解析? 预:提前 解析:编译 预解析通俗的说:js在执行代码之前会读取js代码,会将变量声明提前. 变量声明包含什么?1.var 声明 2.函数的显示声明. 提前 ...

  2. 小程序~列表渲染~key

    如果列表中项目的位置会动态改变或者有新的项目添加到列表中,并且希望列表中的项目保持自己的特征和状态(如 <input/> 中的输入内容, <switch/> 的选中状态),需要 ...

  3. python开发笔记-变长字典Series的使用

    Series的基本特征: 1.类似一维数组的对象 2.由数据和索引组成 import pandas as pd >>> aSer=pd.Series([1,2.0,'a']) > ...

  4. 电脑重装后 python 2 3快速安装

    背景: 电脑重装后,python也要跟着重装,将安装包.环境变量备份等安装完成后直接安装 重装前 1. pip包备份,命令窗口 pip freeze > py2.txt #python2的包 p ...

  5. Hive优化(整理版)

    1. 概述 1.1 hive的特征: 可以通过SQL轻松访问数据的工具,从而实现数据仓库任务,如提取/转换/加载(ETL),报告和数据分析: 它可以使已经存储的数据结构化: 可以直接访问存储在Apac ...

  6. JVM JDK1.8 以后的新特性 VisualVM的安装使用

    一.JVM在新版本的改进更新以及相关知识 1.JVM在新版本的改进更新 图中可以看到运行时常量池是放在方法区的 1.1对比: JDK 1.6 及以往的 JDK 版本中,Java 类信息.常量池.静态变 ...

  7. c#中的多态学习总结

    c#的多台方法,大体上和c++的类似,但是有点区别的,我这里刚刚初学,因此把重点记录下. 多态是同一个行为具有多个不同表现形式或形态的能力. 多态性意味着有多重形式.在面向对象编程范式中,多态性往往表 ...

  8. docker更换源

    $ cd /etc/docker $ cat daemon.json { "registry-mirrors": [ "https://kfwkfulq.mirror.a ...

  9. Spark-源码分析02-Luanch Executor

    1.SparkContext.scala sparkcontext 在被new的时候,会执行class中的代码 其中有一个就是创建TaskScheduler 和 SchedulerBackend,而S ...

  10. “OKR播种机”JOHN DOERR–目标是对抗纷乱思绪的一针疫苗

    OKR培养出疯狂的想法,再加上对的人,奇迹就会出现 约翰·杜尔是美国最有影响力.最具创意.最不拘传统的冒险资本投资家之一.在短短10年内创造了高达1,000亿美元的经济价值.迄今为止,他已向 250家 ...