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. 微信小程序之执行环境

    明白了小程序中的 JavaScript 同浏览器以及NodeJS有所不同后,开发者还需要注意到另外一个问题,不同的平台的小程序的脚本执行环境也是有所区别的. 小程序目前可以运行在三大平台: iOS平台 ...

  2. Python语言程序设计(3)--字符串类型及操作--time库进度条

    1.字符串类型的表示: 三引号可做注释,注释其实也是字符串 2.字符串的操作符 3.字符串处理函数 输出:

  3. 与你一起学习MS Project——基础篇:Project基础应用

    为了更清晰容易地熟悉掌握Project的基础应用,我们在基础篇中一起来学习掌握在Project中如何做进度计划.资源计划.成本计划以及跟踪项目的执行情况并生成所需的项目报表. 一.进度计划 这里,首先 ...

  4. C++定义和初始化数组以及memset的使用(转)

    一.一维数组 静态 int array[100]; 定义了数组array,并未对数组进行初始化 静态 int array[100] = {1,2}: 定义并初始化了数组array 动态 int* ar ...

  5. forword动作

    forword动作   服务器内部跳转指令 语法为: <jsp:forword page = "目标页面"> 等同于:request.getRequestDispatc ...

  6. MySQL server has gone away && Lost connection to MySQL server during query

    问题一.MySQL server has gone away ##### peewee from peewee import * from peewee import __exception_wrap ...

  7. 笨办法学Python

    打印:%r%r 与 %s 的区别就好比 repr() 函数处理对象与 str() 函数处理对象的差别.%s => str(),比较智能%r => repr(),处理较为简单和直接 from ...

  8. Tensorflow细节-P290-命名空间与tensorboard上的节点

    讲解几个重点知识 1.对于tf.get_variable()中的reuse,意思是,如果有名字一模一样的变量,则对这个变量继续使用,如果没有名字一模一样的变量,则创建这个变量 2.options=ru ...

  9. 洛谷P1816 忠诚 题解

    洛谷P1816 忠诚 题解 题目描述 老管家是一个聪明能干的人.他为财主工作了整整10年,财主为了让自已账目更加清楚.要求管家每天记k次账,由于管家聪明能干,因而管家总是让财主十分满意.但是由于一些人 ...

  10. 2016级android在线测试15-图像 camera2

    有趣有内涵的文章第一时间送达! 喝酒I创作I分享 生活中总有些东西值得分享 @醉翁猫咪 1. ImageView类用于显示各种图像,例如:图标.图片,下面对于ImageView类加载图片方法的描述错误 ...