[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 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的更多相关文章
- [LeetCode] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 40. Combination Sum II 组合之和 II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 377. Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] 40. Combination Sum II 组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- 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 ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- LeetCode OJ:Combination Sum II (组合之和 II)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
随机推荐
- Unity进阶之ET网络游戏开发框架 07-修正游客登录的异步BUG
版权申明: 本文原创首发于以下网站: 博客园『优梦创客』的空间:https://www.cnblogs.com/raymondking123 优梦创客的官方博客:https://91make.top ...
- Linux入侵类问题排查思路
深入分析,查找入侵原因 一.检查隐藏帐户及弱口令 检查服务器系统及应用帐户是否存在 弱口令: 检查说明:检查管理员帐户.数据库帐户.MySQL 帐户.tomcat 帐户.网站后台管理员帐户等密码设置是 ...
- 《逆袭团队》第九次团队作业【Beta】Scrum Meeting 1
项目 内容 软件工程 任课教师博客主页链接 作业链接地址 团队作业9:Beta冲刺与团队项目验收 团队名称 逆袭团队 具体目标 (1)掌握软件黑盒测试技术:(2)学会编制软件项目总结PPT.项目验收报 ...
- 通过ip找mac
# coding:utf-8 import os cmd = {'arp': 'arp -a | find "', 'route': 'route PRINT ' } def win_mac ...
- go与python的不同
go 开发中需要注意的与python的不同点 列出golang开发过程中与python的不同点,主要是在语法方面,golang的一些语法真是要人命啊. 1.golang可读性很强,与或对应&& ...
- C博客作业
1.你对网络专业或者计算机专业了解是怎样? 信息化是国企的一个大趋势,目前正是红火的时候. - 网络是信息化必不可少的的基础和平台,随着信息化的进步,网络也必将水涨船高. - 我认为网络方向主要学的是 ...
- samba-centos7
目的: 1,匿名访问共享目录/home/home and /home/share 2,/home/samba/home 共享名为home,有读写权限,但是进入该文件夹需要验证用户 3,/home/sa ...
- jmap -heap 查看堆内存
概述 用jmap -heap命令可以查看linux堆内存分布 具体用法 1:先查出tomcat的进程号 例如: 然后执行 jmap -heap 7095 可以打印出整体的堆信息 可以看到经过分配的 ...
- Ubuntu系统之Hadoop搭建
作业来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/3223 一.在Window中安装Oracle VM VirtualBox 二 ...
- assert(0)的作用
捕捉逻辑错误.可以在程序逻辑必须为真的条件上设置断言.除非发生逻辑错误,否则断言对程序无任何影响.即预防性的错误检查,在认为不可能的执行到的情况下加一句ASSERT(0),如果运行到此,代码逻辑或条件 ...