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 ...
随机推荐
- 来一场说走就走的骑行---23KM的上班探路行动圆满结束
上午带着宝贝在游乐场疯了2小时,回家吃过中午饭,收拾利落,刚上刚拾掇利落的单车,出发,目的地:公司.预测距离22.5KM目的 1 锻炼身体,变每天上下班的娱乐时间为锻炼时间. 2 省钱(其 ...
- 关于sql where id in 转换成数据类型 int 时失败(转)
有执行sql条件语句where id in(@参数)的时候,如果处理不当,就会出现问题:如下面这个存储过程: alter proc Web_gettwtwgoldgameserverGoldSell@ ...
- Java基础相关
对老师上课内容进行总结: 1.新建一个Java项目,并命名为HelloWorld 然后再新建类,并命名为HelloWorld,注意红色画圈部分 若勾选,则新建类开头为(“//后的内容为老师所讲批注”) ...
- IAR MSP430如何生成烧写文件
IAR生成430烧写方法有2种, 第一种是:将工程的debug模式切换成release模式,看图片操作. 那个.d43文件就是仿真调试模式的文件. 这里的test.txt文件就是烧写文件了,不要 ...
- Subversion服务器搭建
如何快速建立Subversion服务器,并且在项目中使用起来,这是大家最关心的问题,与CVS相比,Subversion有更多的选择,也更加的容易,几个命令就可以建立一套服务器环境,可以使用起来,这里配 ...
- C++ little errors , Big problem
---------------------------------------------------------------------------------------------------- ...
- why does txid_current() assign new transaction-id?
Naoya: Hi,hackers! I have a question about txid_current(). it is "Why does txid_current() assig ...
- threading模块和queue模块实现程序并发功能和消息队列
简介: 通过三个例子熟悉一下python threading模块和queue模块实现程序并发功能和消息队列. 说明:以下实验基于python2.6 基本概念 什么是进程? 拥有独立的地址空间,内存,数 ...
- PHP获得两个绝对路径的相对路径
周末在家看面试题,没事儿写了个. 题目: 写一个函数,算出两个文件的相对路径 如 $a = '/a/b/c/d/e.php'; $b = '/a/b/12/34/c.php';计算出 $b 相对于 $ ...
- asp.net系统过滤器、自定义过滤器
原文地址:http://www.cnblogs.com/kissdodog/archive/2013/05/21/3090513.html 一.系统过滤器使用说明 1.OutputCache过滤器 O ...