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. Python之路 day2 文件基础操作

    #!/usr/bin/env python # -*- coding:utf-8 -*- #Author:ersa ''' #f,文件句柄;模式 a : append 追加文件内容 f = open( ...

  2. Tomcat APR & Linux Optimization

    一.简介 APR(Apache portable Run-time libraries)模式:简单理解,就是从操作系统级别解决异步IO问题,大幅度的提高服务器的处理和响应性能, 也是Tomcat运行高 ...

  3. appium案例

    import unittest from time import sleep from appium import webdriver import desired_capabilities clas ...

  4. jq中数组应用的错误

    js中数组可以这样使用: <ul id="ul"> <li value="1">s</li> <li>f< ...

  5. ASP.NET MVC增删改查

    ASP.NET MVC中的增删改查 基本都要使用C控制器中的两个action来完成操作,一个用于从主界面跳转到新页面.同时将所需操作的数据传到新界面,另一个则对应新界面的按钮,用于完成操作.将数据传回 ...

  6. ArcMap Add-in插件开发中解决VS调试时断点不会命中的问题

    在VS2010中进行ArcMap Add-in插件开发(ArcEngine10.1,ArcGIS10.1),运行时为.NET4.0,在程序中设置了断点进行调试,但是运行后程序并不会在断点处停止,且原来 ...

  7. 通过php下载文件并重命名

    $filename = dirname(__FILE__) . '/oldfilename.jpg'; $out_filename = 'newfilename.jpg'; if( ! file_ex ...

  8. Unity Container 应用示例

    一 项目引用Unity 右键项目引用-> 管理Nuget包->搜索unity->安装Unity 和 Unity Interception Extension,如下图所示. 二 创建基 ...

  9. linux php 安装 memcache 扩展

    1. memcached依赖于libevent,需要先安装libevent. tar zxvf libevent-2.0.21-stable.tar.gz cd libevent-2.0.21-sta ...

  10. Rendering Problems:android.support.v7.internal.widget.ActionBarOverlayLayout 解决方法

    不知道是不是android studio安装不对的问题,每次新建项目都有这个问题. 临时解决方法是: 打开 styles.xml 在Theme.AppCompat.Light.DarkActionBa ...