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.

Note:

  • All numbers will be positive integers.
  • The solution set must not contain duplicate combinations.

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

Runtime: 0 ms, faster than 100.00% of C++ online submissions for Combination Sum III.

class Solution {
public:
vector<vector<int>> combinationSum3(int k, int n) {
vector<vector<int>> ret;
vector<int> path;
int target = ;
helper(k, n, , , target, path, ret);
return ret;
}
void helper(const int k ,const int n, int i_th, int prev, int& target, vector<int>& path, vector<vector<int>>& ret){
if(i_th == k && n == target) {
ret.push_back(path);
return ;
}
for(int i=prev+; i < ; i++){
path.push_back(i);
target += i;
helper(k, n, i_th+, i, target, path, ret);
target -= i;
path.pop_back();
}
}
};

LC 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. 【刷题-LeetCode】216. Combination Sum III

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

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

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

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

  7. 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. 216. Combination Sum III——本质DFS

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

  9. 216. Combination Sum III

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

随机推荐

  1. mac上配置apidoc环境

    1. 安装node.js 和npm 前往 https://nodejs.org/en/ 下载node.js的最新版本,双击.pkg进行安装 在终端输入 node -v ,如正确输出版本号即安装成功 ( ...

  2. asp.net mvc5 DataBase First下model校验问题(MetadataType使用)

    最近学习asp.net mvc5,使用   asp.net mvc5+EF6+AutoFac做个小Demo,其中是先设计的数据库表,就直接选择了EF的DataBase First(三种开发模式分别是c ...

  3. SURF算法源代码OPENSURF分析

    SURF算法源代码分析 平台:win x64 + VS2015专业版 +opencv2.4.11 配置类似参考OPENSIFT,参考我的另一篇博客:https://www.cnblogs.com/Al ...

  4. Delphi 类成员的可见性

  5. ContextMenu菜单创建 上下文菜单的基本认识q

    MainActivity.class public class MainActivity extends AppCompatActivity { @Override protected void on ...

  6. vim编辑命令

    vi命令 命令模式: yy:复制 光标所在的这一行 4yy:复制 光标所在行开始向下的4行 p: 粘贴 dd:剪切 光标所在的这一行 2dd:剪切 光标所在行 向下 2行 D:从当前的光标开始剪切,一 ...

  7. Ajax fileUpload

    在项目开发中用到ajax 的 fileUpload,遇到onchange事件只触发一次 原因是fileUpload调用后将原有的file元素改变了,需要早upload后重新绑定元素 第一次绑定: $( ...

  8. [MySQL优化] -- 如何使用SQL Profiler 性能分析器

    mysql 的 sql 性能分析器主要用途是显示 sql 执行的整个过程中各项资源的使用情况.分析器可以更好的展示出不良 SQL 的性能问题所在. 下面我们举例介绍一下 MySQL SQL Profi ...

  9. 洛谷P4001 [BJOI2006]狼抓兔子(平面图转对偶图)

    传送门 明明只要最小割加点优化就能过的东西…… 然而我偏偏要去学平面图转对偶图结果发现课件关键地方看不清->这里 而且建图累的半死…… 说实话只要最大流建图的时候反向边直接设为当前边容量再加个当 ...

  10. BZOJ 3217: ALOEXT (块状链表套trie)

    第一次写块状链表,发现还挺好写的,但是一点地方写错加上强制在线就会各种姿势WA/TLE/RE爆- 想法就是分块后,在每一个块上维护最大值和次大值,还在每一个块上维护一棵trie树来求异或最大值.散块直 ...