216 Combination Sum III 组合总和 III
找出所有可能的 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的更多相关文章
- 040 Combination Sum II 组合总和 II
给定候选号码数组 (C) 和目标总和数 (T),找出 C 中候选号码总和为 T 的所有唯一组合.C 中的每个数字只能在组合中使用一次.注意: 所有数字(包括目标)都是正整数. 解决方案集不 ...
- 【LeetCode】Combination Sum II(组合总和 II)
这道题是LeetCode里的第40道题. 题目要求: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. can ...
- Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III)
Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III) 同类题目: Leetcode之回溯法专题-39. 组合总数(Combination Sum) Lee ...
- [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 ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- Java实现 LeetCode 216. 组合总和 III(三)
216. 组合总和 III 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. 解集不能包含重复的组合. ...
- 216. 组合总和 III
216. 组合总和 III 题意 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. 解集不能包含重复的 ...
- [LeetCode] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- 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 ...
随机推荐
- 【机器学习具体解释】SVM解二分类,多分类,及后验概率输出
转载请注明出处:http://blog.csdn.net/luoshixian099/article/details/51073885 CSDN−勿在浮沙筑高台 支持向量机(Support Vecto ...
- java 生成压测数据
询价接口压测,需要批量生成数据, 数据包括4个字段(车牌号,车架号,发动机号,支付号)licenseNo,vehicleFrameNo,engineNo,payFlowId 需符合LoadRunner ...
- 实现@using{}代码块
前几天,我要写个插件,放在asp.net mvc的view上.这需要写一些扩展HtmlHelper功能的方法.这些方法的一个参数是一段javascript代码. 假如这个参数类型是字符型,当然很简单了 ...
- 2016/4/7 datatype:①json ②XML
①JSON 1,postjsonxml.php json用循环方式处理传来的值 for(key in data) for(var i=0;i<data.length;i++){data ...
- StyleBook皮肤控件的使用
StyleBook 介绍及VICEN对皮肤控件的一些看法可以说StyleBook的出现,简直是皮肤控件厂商的噩梦,因为用户可以通过StyleBook快速切换控件样式,而不需要在去购买第三方换肤控件,对 ...
- ABAP 读取服务器CSV文件到内表
DATA: BEGIN OF gs_data , cola TYPE string, colb TYPE string, ... END OF gs_data, gt_data LIKE TABLE ...
- swift中的@objc的作用
转载:https://www.jianshu.com/p/6c5b45d9d042 自动清除冗余代码减小包大小 得益于 Swift 的静态语言特性,每个函数的调用在编译期间就可以确定.因此在编译完成后 ...
- XMU 1613 刘备闯三国之三顾茅庐(一) 【并查集】
1613: 刘备闯三国之三顾茅庐(一) Time Limit: 1000 MS Memory Limit: 128 MBSubmit: 99 Solved: 29[Submit][Status][ ...
- HDU1160 FatMouse's Speed —— DP
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1160 FatMouse's Speed Time Limit: 2000/1000 MS ...
- UVA796 Critical Links —— 割边(桥)
题目链接:https://vjudge.net/problem/UVA-796 In a computer network a link L, which interconnects two serv ...