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的更多相关文章

  1. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

    39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...

  2. [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 ...

  3. 【LeetCode】216. Combination Sum III

    Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...

  4. LC 216. Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  5. 【刷题-LeetCode】216. Combination Sum III

    Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...

  6. 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV

    ▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...

  7. 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 ...

  8. Leetcode 216. Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  9. 216. Combination Sum III——本质DFS

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

随机推荐

  1. c语言中函数的递归

    题目:用递归法把一个整数转换成字符串输出. 比较下面两种方法的不同: putchar(n%10+'0')的位置不同,造成输出结果的不同. 方法一: #include <stdio.h> v ...

  2. dbcp基本配置和重连配置 -- mysql 8小时自动断开连接的问题

    1. 引入dbcp (选择1.4) Java代码   com.alibaba.external jakarta.commons.dbcp 1.4 2. dbcp的基本配置 相关配置说明: initia ...

  3. js部分---运算符,if分支语句,for循环;switch case 的用法;

    ------------------------------------------运算符---------------------------------------------------- *数 ...

  4. c#---部分;把数组或者结构体存入集合里,然后再从集合中取出之后,输出;foreach既可以用到提取数组重点额数据,也可以提取集合中的数据(前提是集合中的元素是相同数据类型)

    1.输入班级人数,统计每个人的姓名,性别,年龄:集合与数组 //Console.Write("请输入班级人数:"); //int a = int.Parse(Console.Rea ...

  5. Codeforces Round #133 (Div. 2)

    A. Tiling with Hexagons 看成大三角形扣去3个小三角形. B. Forming Teams 由于每个点的度数不超过2,所以最后每个点要么在一条链上要么在一个环上. 在环上的话,每 ...

  6. codeforces 725/C

    Hidden Word time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  7. kuangbin_UnionFind C (HDU 1213)

    过程模板 扫一下一共有几棵树 输出 #include <iostream> #include <string> #include <cstdio> #include ...

  8. Oracle升级前备份和失败回退

    一.升级前备份 1.软件备份[root@localhost backup]# su - oracle [oracle@localhost ~]$ cd $ORACLE_HOME[oracle@loca ...

  9. Linux实时监控工具Nmon使用

    官网:http://nmon.sourceforge.net/pmwiki.php?n=Main.HomePage 下载:http://sourceforge.net/projects/nmon/fi ...

  10. HTML和JSON的数据交互-HTML模板

    直接上源码,原文http://www.zhangxinxu.com/wordpress/2012/09/javascript-html-json-template/ <!DOCTYPE html ...