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. Android 三大图片加载框架的对比——ImageLoader,Picasso,Glide

    一.ImageLaoder介绍 << Universal ImageLoader 是很早开源的图片缓存,在早期被很多应用使用 多线程下载图片,图片可以来源于网络,文件系统,项目文件夹ass ...

  2. Oracle ORA-12154: TNS: 无法解析指定的连接标识符”错误

    主要原因: 1.监听服务没有起起来.windows平台个一如下操作:开始---程序---管理工具---服务,打开服务面板,启动oraclehome92TNSlistener服务. 2.database ...

  3. 个人对JQuery Proxy()函数的理解

    转载至:http://www.cnblogs.com/acles/archive/2012/11/20/2779282.html JQuery.proxy(function,context): 使用c ...

  4. eclipse不显示Android SDK Manager标签

    新版的eclipse配置好android开发环境后没有显示在window菜单里显示Android SDK Manager,也没有在工具栏里出现android的工具图标.但可以通过android sdk ...

  5. Sprint 2

    成员 团队贡献分 许佳仪 22 柯晓君 23 卓宇靖 18 赖文亮 17 浏览书籍 查询书籍(可分别按照图书名和价格进行查询)

  6. 工作需求----表单select多选交互

    由于工作需求接触select框多选的情况,以下是我分享的代码,主要是进入页面默认选中.支持多选属性: 1.html内容 multiple=”multiple” 属性为多选属性 <div clas ...

  7. 一个crackme的分析

    是看雪合集的一个,因为老师让我们多练习,所以我就找了个crackme来练习 http://images2015.cnblogs.com/blog/638600/201612/638600-201612 ...

  8. phonegap + xcode5.0.2 配置开发环境

    phonegap官网:  http://phonegap.com/ 第一部:安装nodejs 安装地址:http://nodejs.org/ 安装phoneGap 官网下载http://phonega ...

  9. [并查集] POJ 2236 Wireless Network

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 25022   Accepted: 103 ...

  10. SpringMVC 表单标签 & 处理静态资源

    使用 Spring 的表单标签 通过 SpringMVC 的表单标签可以实现将模型数据中的属性和 HTML 表单元素相绑定,以实现表单数据更便捷编辑和表单值的回显. form 标签 一般情况下,通过 ...