216. Combination Sum 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]]
题目:从1-9个数字中,找出k个数,是k个数的和为n,
返回所有k个数字所有的组合,每一个组合中k个数字都是不相同的.
思路:
深度优先搜索dfs+剪枝,
leetecode上的大部分递归题目,都会利用这种方式的写法
===============
code:
时间复杂度:O(n!),会出现n!总组合
空间复杂度:O(n),递归会n层
class Solution {
public:
void help_cbs3dfs(int k,int n,int level,vector<int> &out,vector<vector<int>> &re){
//@k,每一组合的数量
//@n,
//@level,从何处开始搜索
//@out,保存搜索路径
//@re,最终结果
if(n<) return;
if(n== && (int)out.size()==k) re.push_back(out);
for(int i = level;i<=;i++){
out.push_back(i);
help_cbs3dfs(k,n-i,i+,out,re);
out.pop_back();
}
}
vector<vector<int>> combinationSum3(int k,int n){
vector<vector<int>> re;
vector<int> out;
help_cbs3dfs(k,n,,out,re);
return re;
}
};
==
216. Combination Sum III的更多相关文章
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- [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】216. Combination Sum III
Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...
- LC 216. Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- 【刷题-LeetCode】216. Combination Sum III
Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...
- 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV
▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...
- 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 216. Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- 216. Combination Sum III——本质DFS
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
随机推荐
- 110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 116. Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- Codeforces Round #365 (Div. 2) A 水
A. Mishka and Game time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- bzoj 1012 维护一个单调数列
Description 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度. 2. ...
- Qt 串口学习3
1.怎样将选择波特率 1)使用if else 的方法 if(ui->BaudRate->currentText()==tr("9600")) //根据组合框 ...
- 动态加载dll,并创建类对象放入到list中。
private List<IVisualControlsPlug> visualPlugs = new List<IVisualControlsPlug>(); public ...
- InvokeRequired和Invoke
C#中禁止跨线程直接访问控件,InvokeRequired是为了解决这个问题而产生的,当一个控件的InvokeRequired属性值为真时,说明有一个创建它以外的线程想访问它.此时它将会在内部调用ne ...
- java 类型转json格式
json-lib-2.4-jdk15.jar List<People> mapPersonTypes = null; private JSONArray json_mapPersonTyp ...
- 023. Asp.net参数化查询预防Sql注入攻击
/// <summary> /// 参数化查询预防SQL注入式攻击 /// </summary> public int checkLogin(string loginName, ...
- 【转】jQuery选择器总结
jQuery 的选择器可谓之强大无比,这里简单地总结一下常用的元素查找方法 $("#myELement") 选择id值等于myElement的元素,id值不能重复在文档中 ...