LeetCode OJ:Combination Sum III(组合之和III)
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)的更多相关文章
- [LeetCode] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 40. Combination Sum II 组合之和 II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 377. Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] 40. Combination Sum II 组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [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 ...
- LeetCode OJ:Combination Sum II (组合之和 II)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- [Leetcode] combination sum ii 组合之和
Given a collection of candidate numbers ( C ) and a target number ( T), find all unique combinations ...
随机推荐
- Hadoop权威指南读书笔记
本书中提到的Hadoop项目简述 Common:一组分布式文件系统和通用I/O的组件与接口(序列化.javaRPC和持久化数据结构). Avro:一种支持高效.跨语言的RPC以及永久存储数据的序列化系 ...
- Java转C#,非常不错(转)
http://www.cnblogs.com/cnwebman/archive/2012/07/21/2602436.html 最近正在研究将一个纯java工程如何转换成C#工程,代码量还比较大,于是 ...
- OS X 与传统Unix的一点区别
在传统的Unix系统或者Linux系统中,你是很难在根目录下找到大写开头的文件夹的, 但是看一下OS X: ls / Applications Users etc private var Develo ...
- 027_编写MapReduce的模板类Mapper、Reducer和Driver
模板类编写好后写MapReduce程序,的模板类编写好以后只需要改参数就行了,代码如下: package org.dragon.hadoop.mr.module; import java.io.IOE ...
- Windows虚拟机安装Linux系统
windows系统安装linux centos虚拟系统 1.下载 VMware Workstation Pro并安装,效果如图 2.下载linux系统 https://www.centos.org/d ...
- Array.asList:数组转list
String s[]={"aa","bb","cc"}; List<String> sList=Arrays.asList(s) ...
- shell set 命令
用set命令可以设置各种shell选项或者列出shell变量.单个选项设置常用的特性.在某些选项之后-o参数将特殊特性打开.在某些选项之后使用+o参数将关闭某些特性,不带任何参数的set命令将显示sh ...
- 20145231 《Java程序设计》第一次实验
实验一 Java开发环境的熟悉(Windows+IDEA) 实验内容 使用JDK编译.运行简单的Java程序: 使用IDEA编辑.编译.运行.调试java程序: 实验知识点 JVM.JRE.JDK的安 ...
- Qt编译器
有两种,MSVC和MINGW Qt 中有两种方式编译,一种是MinGW ,另一种MSVC. MSVC是指微软的VC编译器: MingGW是指是Minimalist GNU on Windows的缩写. ...
- CCNA 课程 七
WAN(Wide Area Network)广域网 运行在OSI模型的数据链路层.物理层. 数据链路层的协议主要有: HDLC (High-Level Data Link Control 高级数据链 ...