找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字。

说明:

  • 所有数字都是正整数。
  • 解集不能包含重复的组合。

示例 1:

输入: k = 3, n = 7 输出: [[1,2,4]]

示例 2:

输入: k = 3, n = 9 输出: [[1,2,6], [1,3,5], [2,3,4]]

class Solution {
public:
vector<vector<int> > res;
vector<vector<int> > combinationSum3(int k, int n)
{
if(n == 0 || k == 0)
return res;
vector<int> temp;
DFS(1, k, n, temp);
return res;
} void DFS(int pos, int count, int val, vector<int> &v)
{
if(count == 0 && val == 0)
{
res.push_back(v);
}
if(count == 0 || val <= 0)
{
return;
}
for(int i = pos; i <= 9; i++)
{
if(val - i < 0)
{
break;
}
v.push_back(i);
DFS(i + 1, count - 1, val - i, v);
v.pop_back();
}
}
};

Leetcode216. Combination Sum III组合总数3的更多相关文章

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

  2. [LeetCode] Combination Sum III 组合之和之三

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

  3. 216 Combination Sum III 组合总和 III

    找出所有可能的 k 个数,使其相加之和为 n,只允许使用数字1-9,并且每一种组合中的数字是唯一的.示例 1:输入: k = 3, n = 7输出:[[1,2,4]]示例 2:输入: k = 3, n ...

  4. [LeetCode] 377. Combination Sum IV 组合之和 IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  5. Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III)

    Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III) 同类题目: Leetcode之回溯法专题-39. 组合总数(Combination Sum) Lee ...

  6. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  7. Combination Sum,Combination Sum II,Combination Sum III

    39. Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique co ...

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

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

  9. [LeetCode] 377. Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

随机推荐

  1. Lost's revenge HDU - 3341 AC自动机+DP(需要学会如何优雅的压缩状态)

    题意: 给你n个子串和一个母串,让你重排母串最多能得到多少个子串出现在重排后的母串中. 首先第一步肯定是获取母串中每个字母出现的次数,只有A T C G四种. 这个很容易想到一个dp状态dp[i][A ...

  2. mvc中在cshtml页面中如何访问静态页面.html

    把静态页面放到@Url.Content();中,如href="@Url.Content("a.html")"

  3. vue项目导出EXCEL功能

    因为一些原因导出EXCEL功能必须前端来做,所以就研究了一下,在网上也找了一些文章来看,有一些不完整,我做完了就记录下来,供大家参考: 1.首先先安装依赖: npm install file-save ...

  4. 读书笔记 | 敏捷编码&敏捷调试

    这周的个人项目让我感受到自己在编程方面的不足和缺陷,所以选择了<高效程序员的45个习惯>中的敏捷开发和敏捷调试两个章节进行阅读. 以下将对敏捷开发和敏捷调试展开详述. [敏捷开发] 注释 ...

  5. spring-data-JPA repository自定义方法规则

    一.自定义方法的规则 Spring Data JPA框架在进行方法名解析时,会先把方法名多余的前缀截取掉,比如find,findBy,read,readBy,get,getBy,然后对剩下的部分进行解 ...

  6. LINUX挂接移动硬盘

    对linux系统而言,USB接口的移动硬盘是当作SCSI设备对待的.插入移动硬盘之前,应先用fdisk –l 或 more /proc/partitions查看系统的硬盘和硬盘分区情况. [root ...

  7. oi知识表

  8. 【学术篇】luogu1558&&poj2777 色板游戏

    题目の传送门: luogu:https://www.luogu.org/problem/show?pid=1558 poj:http://poj.org/problem?id=2777 题目大意:给有 ...

  9. 【笔记篇】C#笔记3

    笔记目录:http://blog.csdn.net/enzymii/article/details/77169928 C#的接口有点意思,我们说过可以用来多重继承.. using System; na ...

  10. Eclipse 常用快捷键 (动画讲解)【转】

    Eclipse 常用快捷键 (动画讲解)[转] Eclipse有强大的编辑功能, 工欲善其事,必先利其器, 掌握Eclipse快捷键,可以大大提高工作效率. 小坦克我花了一整天时间, 精选了一些常用的 ...