https://leetcode.com/problems/combination-sum-iii/

用dfs枚举。

class Solution {
public:
int kk, nn;
vector<vector<int>> res;
vector<vector<int>> combinationSum3(int k, int n) {
kk = k, nn = n;
vector<int> r;
dfs(r, , );
return res;
} void dfs(vector<int> &a, int t, int sum) {
if (a.size() >= kk || sum >= nn) {
if (a.size() == kk && sum == nn) {
res.push_back(a);
}
return;
}
for (int i = t + ; i < ; ++i) {
a.push_back(i);
dfs(a, i, sum + i);
a.pop_back();
}
}
};

其他同类题:

http://www.jiuzhang.com/solutions/search/?question=Combination+Sum

1.  http://www.lintcode.com/zh-cn/problem/letter-combinations-of-a-phone-number/

class Solution {
public:
vector<string> ret; vector<char> getChar(int num) {
vector<char> ret;
if (num == ) return ret;
if (num < && num > ) {
ret.push_back('a' + (num - ) * );
ret.push_back('a' + (num - ) * + );
ret.push_back('a' + (num - ) * + );
} else if (num == ) {
ret.push_back('p');
ret.push_back('p' + );
ret.push_back('p' + );
ret.push_back('p' + );
} else if (num == ) {
ret.push_back('t');
ret.push_back('t' + );
ret.push_back('t' + );
} else if (num == ) {
ret.push_back('w');
ret.push_back('w' + );
ret.push_back('w' + );
ret.push_back('w' + );
}
return ret;
} void dfs(string& s, int t, string &d) {
if (t == d.size() && s.size() > ) {
ret.push_back(s);
return;
}
vector<char> cs = getChar(d[t] - '');
for (int i = ; i < cs.size(); ++i) {
s.push_back(cs[i]);
dfs(s, t + , d);
s.pop_back();
}
} vector<string> letterCombinations(string& digits) {
// Write your code here
string s = "";
dfs(s, , digits);
return ret;
}
};

leetcode216-Combination Sum III的更多相关文章

  1. Leetcode216. Combination Sum III组合总数3

    找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. 解集不能包含重复的组合. 示例 1: 输入: k = ...

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

  3. [Leetcode 216]求给定和的数集合 Combination Sum III

    [题目] Find all possible combinations of k numbers that add up to a number n, given that only numbers ...

  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. leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III

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

  6. Leetcode题解(4):L216/Combination Sum III

    L216: Combination Sum III Find all possible combinations of k numbers that add up to a number n, giv ...

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

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

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

  9. LC 216. Combination Sum III

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

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

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

随机推荐

  1. jq变态全选vs原生变态全选

    <script> $(function(){ var num=0; $("#btn").on('click',function(){ if(this.checked){ ...

  2. SQL server 常用语句

    SQL Server中常用的SQL语句   1.概述 2.查询概述 3.单表查询 4.连接查询 5.带有exists的相关子查询 6.SQL的集合操作 7.插入操作 8.删除操作 9.修改操作 10. ...

  3. 点开看看please

  4. SQLSERVER和ORACLE系统表获取表名 列名以及列的注释

    在工作中从数据库取的数据要导出来,但是发现导出的EXCEL中列名都是字段名(英文),为此搜集资料怎么把字段名变为中文名称,而发现ORACLE和SQLSERVER(用的SQLSERVER2008R2)又 ...

  5. 伸缩盒子模型,旧的伸缩盒子模型。浏览器内核、css继承属性

  6. 编译OpenJDK的笔记

    1.  ERROR: You seem to not have installed ALSA 0.9.1 or higher. 不需要从ALSA官网下载alsa-dev和alsa-drive, ubu ...

  7. C# Request中修改header信息

    var headers = app.Context.Request.Headers; Type hdr = headers.GetType(); PropertyInfo ro = hdr.GetPr ...

  8. 在Ubuntu环境把PPT和Word转换为swf文件

    项目需要一个在线浏览文档的功能,于是参照网上的代码写了一份利用Microsoft Office 2010和swftools-2013-04-09-1007.exe转换的程序 思路:调用电脑本机的off ...

  9. 多线程下的for循环问题

    List<int> _ValueLis = new List<int>(); private void AddInt(int i) { _ValueLis.Add(i); } ...

  10. GridView获取列子段的几种途径

    GridView是ASP.NET中功能强大的数据显示控件,它的RowDataBound事件为我们提供了方便的控制行.列数据的途径. 要获取当前行的某个数据列,我在实践中总结有如下几种方法: 1. Ce ...