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. 记一次对上传对jsp限制的绕过

    当访问网站任何.jsp后缀的文件时都会显示如下图所示或者session timeout等提示, 并且网站防护会,对上传大马和一句话会被查杀. 解决方法: 利用jspx包含,利用jspx包含图片或者cs ...

  2. AD-logon workstation

    默认AD登录到限制为64个 原因 发生此问题的原因是User-Workstations属性的Range-Upper值为1,024个字符.使用Active Directory用户和计算机输入NetBIO ...

  3. 0031ActiveMQ的下载安装与启动

    消息中间件activemq的作用主要是解耦.异步.削峰. 我们按如下步骤详细讲解一下activemq的下载.安装与启动. 1.activemq的下载 下载地址: http://activemq.apa ...

  4. 项目Alpha冲刺(团队)-测试篇

    格式描述 课程名称:软件工程1916|W(福州大学) 作业要求:项目Alpha冲刺(团队)-代码规范.冲刺任务与计划 团队名称:为了交项目干杯 测试用例:测试用例文档.zip 作业目标:描述项目的测试 ...

  5. Python常用标准库函数

    math库: >>> import math >>> dir(math) ['__doc__', '__loader__', '__name__', '__pack ...

  6. django-使用类视图

    视图函数views.py中 from django.shortcuts import render, redirect from django.http import HttpResponse, Js ...

  7. linux ssh tunnel

    ssh -qTfnN -D 7070 ape@192.168.1.35

  8. Codeforces 1251E Voting

    E2. Voting (Hard Version) 题意: 有n个人, 你想让他们都给你投票. 你可以选择花费pi收买第i个人, 或者如果有mi个人已经给你投票了, 那么第i个人会自动给你投票. 不妨 ...

  9. 应用安全测试技术DAST、SAST、IAST对比分析【转】

    转自:https://blog.csdn.net/qq_29277155/article/details/92411079 一.全球面临软件安全危机 2010年,大型社交网站rockyou.com被曝 ...

  10. less简介及其编译原理

    一.less环境安装 ①首先需要在电脑上安装nodejs,一般会内置npm,利用以下命令可以检测: ②利用npm在线安装less,运行  npm install –g less ③查看是否安装成功,L ...