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

关于 backtrack problem 做的最快的一次.

定义了2个东西:

  • start
  • remain

自己想法,自己代码:

vector<vector<int>> combinationSum3(int k, int n) {
vector<int> A = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
vector<vector<int> > res;
vector<int> temp;
backtrack(res, temp, A, k, 0, n);
return res;
} void backtrack(vector<vector<int> >& res, vector<int>& temp, vector<int>& A,
int k, int start, int remain) {
if (temp.size() == k && remain == 0) {
res.push_back(temp);
return;
}
for (int i = start; i < A.size(); i++) {
temp.push_back(A[i]);
backtrack(res, temp, A, k, ++start, remain - A[i]);
temp.pop_back();
}
}

216. Combination Sum III(medium, backtrack, 本类问题做的最快的一次)的更多相关文章

  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. LeetCode 216. Combination Sum III (组合的和之三)

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

随机推荐

  1. angular2 学习笔记 ( app initialize 初始化 )

    refer : http://stackoverflow.com/questions/39033835/angularjs2-preload-server-configuration-before-t ...

  2. c++中的类之构造函数

    一.构造函数的缘由 本文我们主要来讲解c++中类的构造函数,其中涉及了深拷贝和浅拷贝的问题,这也是在面试笔试中经常会碰到的问题.如果您是第一次听说构造函数,可能会觉得这个名字有点高大上,而它却和实际中 ...

  3. 安装shellinabox-master

    安装shellinabox-master 1 插件介绍:通过web页面管理linux主机(电脑版) a. 安装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...

  4. hdu1003 Max Sum---最大子段和+记录开始结束点

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目大意: 求最大子段和,并且输出最大子段和的起始位置和终止位置. 思路: 根据最大子段和基本 ...

  5. MongDB配置方法

    先下载安装包 安装 方法一:命令行启动 在 C:\MongoDB\Server\3.4目录下建立data文件夹, data里面建立db文件夹 bin目录下运行 mongod --dbpath C:\M ...

  6. STL迭代器------Traits编程技法详细理解(一)

    最近在看STL源码解析的迭代器(iterators)一章,涉及到c++ Traits的编程技法,刚开始看时一头雾水,反复看了好几遍之后才理解这个东西,因此来写写在这方面的理解,如有错误,希望读者指正. ...

  7. 关于阮大神的es6标准入门第一章

    题记:之前在10月份的时候写过阮大神的es6的第一章,但是由于那段时间项目组的动荡,所以也没有什么后续,导致我现在对es6基本都忘的差不多了,不过,现在换了新公司,最近也没什么任务,所以现在开始重新写 ...

  8. oracle11.2中分区功能测试之add&amp;split partition对global&amp;local index的影响

    生产库中某些大表的分区异常,需要对现有表进行在线操作,以添加丢失分区,因为是生产库,还是谨慎点好,今天有空,针对add&split分区对global&local索引的影响进行了测试,测 ...

  9. [LeetCode] Cherry Pickup 捡樱桃

    In a N x N grid representing a field of cherries, each cell is one of three possible integers. 0 mea ...

  10. 用js来实现那些数据结构10(集合02-集合的操作)

    前一篇文章我们一起实现了自定义的set集合类.那么这一篇我们来给set类增加一些操作方法.那么在开始之前,还是有必要解释一下集合的操作有哪些.便于我们更快速的理解代码. 1.并集:对于给定的两个集合, ...