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. swift_简单值 | 元祖 | 流程控制 | 字符串 | 集合

    //: Playground - noun: a place where people can play import Cocoa var str = "Hello, playground& ...

  2. 使用script创建标签添加属性值和添加样式

    <mark>使用script创建标签和给标签属性值以及样式的方法</mark><script> window.onload=function(){ var btn= ...

  3. postgres 正则表达式 转

    http://blog.csdn.net/wugewuge/article/details/7704996 postgresql中使用正则表达式时需要使用关键字“~”,以表示该关键字之前的内容需匹配之 ...

  4. Python笔记总结week6

    关于创建.调用模块 1.我们创建一个模块commons.py,  并且在文件中写以下三个函数: def login(): print('登录') def logout(): print('退出') d ...

  5. SQL Server数据库备份:通过Windows批处理命令执行

    通过Windows批处理命令执行SQL Server数据库备份 建立mybackup.bat ,输入以下内容直接运行该脚本,即可开始自动备份数据库也可把该脚本加入windows任务计划里执行. --- ...

  6. F2工作流引擎这工作流引擎体系架构(二)

    F2工作流体系架构概览图 为了能更好的了解F2工作流引擎的架构体系,花了些时间画了整个架构的体系图.F2工作流引擎遵循参考WFCM规范,目标是实现轻量级的工作流引擎,支持多种数据库及快速应用到任何基于 ...

  7. PLSQL大数据生成规则

    数据定义 数据定义决定了被生成的数据.如果要创建简单的字符,可以在两个方括号之间输入字符定义:[数据] 数据可以是下列预先确定的集的混合体:           •  a: a..z (小写字符)   ...

  8. C#中REF和OUT的区别

    在C# 中,既可以通过值也可以通过引用传递参数.通过引用传递参数允许函数成员更改参数的值,并保持该更改.若要通过引用传递参数, 可使用ref或out关键字.ref和out这两个关键字都能够提供相似的功 ...

  9. JS数组常用函数以及查找数组中是否有重复元素的三种常用方法

    阅读目录: DS01:常用的查找数组中是否有重复元素的三种方法 DS02:常用的JS函数集锦 DS01.常用的查找数组中是否有重复元素的三种方法  1. var ary = new Array(&qu ...

  10. jquery 巧用json传参

    JavaScript代码,巧用JSON传参数function AddComment(content) { var comment = {}; comment.threadId = $("#s ...