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 ...
随机推荐
- ios 避免两个button同一时候被点击
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/superchaoxian/article/details/24631293 这个能够通过[btn ...
- Android学习笔记之AndroidManifest.xml文件解析(详解)
一.关于AndroidManifest.xml AndroidManifest.xml 是每个android程序中必须的文件.它位于整个项目的根目录,描述了package中暴露的组件(activiti ...
- SaltStack任务计划
编辑fansik_cron.sls文件: 内容如下: cron_test: cron.present: - name: /bin/touch /tmp/fansik.txt - user: root ...
- PHP实现生成唯一编号(36进制的不重复编号)
当我们要将一个庞大的数据进行编号时,而编号有位数限制,比如5位的车牌号.10位的某证件号码.订单流水号.短网址等等,我们可以使用36进制计算出符合位数的不重复的编号. 我们将0-Z(012345678 ...
- sublime text3自动同步左边栏颜色背景为编辑栏颜色
下面的步骤需要安装Package Control插件,如果你已经安装,可跳过本步骤,直接看第二步. 第一步:安装Package Control插件: 按Ctrl+`调出console(注:安装有QQ输 ...
- XML文件结构和基本语法
XML文件的结构性内容,包括节点关系以及属性内容等等.元素是组成XML的最基本的单位,它由开始标记,属性和结束标记组成.就是一个元素的例子,每个元素必须有一个元素名,元素可以若干个属性以及属性值. x ...
- linux开发应用程序到运行的过程
1.给linux配置交叉编译环境 2.在windows下使用source insight写程序,一共有m个h文件,n个c文件,k个main函数 3.将源代码放在linux里 4.在源代码文件夹里新建m ...
- HTML5 JS 压缩图片,并取得图片的BASE64编码上传
基本过程 1) 调用 FileReader 的 reader.readAsDataURL(img); 方法, 在其onload事件中, 将用户选择的图片读入 Image对象. 2) 在image对象的 ...
- Javascript中一些常用的宽与高
在使用javascript制作一些网络特效时,往往要根据显示网页的显示器的一些参数展开进行.所以一些关于显示器的参数如何得到显得十分重要.下面是一些常用的显示器参数,不妨好好记一下吧! 网页可见区域宽 ...
- tp3.2关联模型 BELONGS_TO
<?php namespace Home\Model; use Think\Model\RelationModel; class AttenModel extends RelationModel ...