Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.

Ensure that numbers within the set are sorted in ascending order.

就是去k个数的和加起来等于n的组合的个数,数字只能取1-9,做法比较简单就是DFS吗,这里不再赘述,我比较喜欢用private变量,这样函数的参数式写出来比较简单:

 class Solution {
public:
vector<vector<int>> combinationSum3(int k, int n){
size = k;
target = n;
vector<int> tmpVec;
tmpVec.clear();
ret.clear();
dfs(tmpVec, target, );
return ret;
}
void dfs(vector<int>& vec, int left, int start)
{
if(left < ) return;
if(left == && vec.size() == size){
ret.push_back(vec);
return;
}
for(int i = start; i <= ; ++i){
vec.push_back(i);
dfs(vec, left - i, i + );
vec.pop_back();
}
}
private:
vector<vector<int>> ret;
int target;
int size;
};

java版本的如下所示,方法仍然相同,简单的DFS:

 public class Solution {
public List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
List<Integer> tmp = new ArrayList<Integer>();
dfs(ret, tmp, 0, k, 1, n);
return ret;
} public void dfs(List<List<Integer>>ret, List<Integer>tmp, int dep, int maxDep, int start, int left){
if(left < 0)
return; //回溯
else if(left == 0){
if(dep == maxDep)
ret.add(new ArrayList<Integer>(tmp));
return;
}else{
for(int i = start; i <= 9; ++i){
tmp.add(i);
dfs(ret, tmp, dep+1, maxDep, i+1, left - i);
tmp.remove(new Integer(i));
}
}
}
}

LeetCode OJ:Combination Sum III(组合之和III)的更多相关文章

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

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

  2. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  3. [LeetCode] 40. Combination Sum II 组合之和 II

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

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

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

  5. [LeetCode] 40. Combination Sum II 组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

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

  7. LeetCode OJ:Combination Sum II (组合之和 II)

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

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

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

  9. [LeetCode] Combination Sum II 组合之和之二

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  10. [Leetcode] combination sum ii 组合之和

    Given a collection of candidate numbers ( C ) and a target number ( T), find all unique combinations ...

随机推荐

  1. windows 安装 python _ flask

    1:首先安装python虚拟环境;(略) 2: 添加一个虚拟环境: 在你的项目目录里直接 virtualenv venv 启动虚拟环境;\venv\Scripts 直接运行activate 3: 在虚 ...

  2. Python2 socket TCPServer 多线程并发 超时关闭

    在阿里云上测试过,可以直接使用. 用IP和端口发送数据,会返回echo:+接收到的数据 #coding=utf-8 import socket import threading,getopt,sys, ...

  3. Amazon2014在线笔试 第三题

    问题描述: 算法分析: s1:层数对齐:分别求两个数所在的层(l1,l2),把层数大的(假设l2>l1)先往上找父节点,从而对齐到l1层: s2:两个数同时往上找, 直到找到公共的父节点(一定能 ...

  4. Multi-thread & Multi-process

    关于多进程和多线程,教科书上最经典的一句话是“进程是资源分配的最小单位,线程是CPU调度的最小单位”. 对于到底是使用多进程还是多线程, 要根据实际情况来判断,选择更适合的. 具体情况,可以参考下面: ...

  5. C语言定义一个指针变量

    10.2.1 定义一个指针变量 对指针变量的定义包括三个内容: (1) 指针类型说明,即定义变量为一个指针变量: (2) 指针变量名: (3) 变量值(指针)所指向的变量的数据类型. 其一般形式为: ...

  6. win10 chrome 调试

      下载NPAPI版本的flash player: http://www.adobe.com/support/flashplayer/debug_downloads.html#fp13       禁 ...

  7. JavaWeb:Tomcat服务器的安装与配置

    Tomcat服务器的安装与配置 安装 输入网址进入Tomcat的官网            在左边导航栏选择对应下载的版本            下载安装包形式            下载并解压到我们 ...

  8. CSS3 3D旋转按钮对话框

    在线演示 本地下载

  9. iOS_CLLocation定位

      CoreLocation框架可用于定位设备当前经纬度,通过该框架,应用程序可通过附近的蜂窝基站,WIFI信号或者GPS等信息计算用户位置.      iOS定位支持的3种模式.      (1)G ...

  10. 写makefile时候的cc和gcc

    Linux 下 的 cc 和 gcc     Linux 下 的 cc 和 gcc 周银辉 在Linux下一会看到cc,另一会又看到gcc,感觉又点混乱的样子.它们是同一个东西么,有啥区别呢 一分为二 ...