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 ...
随机推荐
- 蒙特卡罗树搜索(MCTS)【转】
简介 最近AlphaGo Zero又火了一把,paper和各种分析文章都有了,有人看到了说不就是普通的Reinforcement learning吗,有人还没理解估值网络.快速下子网络的作用就放弃了. ...
- Eclipse中svn操作
1.主干和分支间合并代码 合并根据目标不同分为2种: 1.分支合并到主干:主要用在修复完生产BUG,并上线之后.需把改动的代码合并到主干上. 2.主干合并到分支:公用的逻辑改动,需反映到所有并行的分支 ...
- HAProxy配置参数说明
一.全局配置"global"配置中的参数为进程级别的参数,且通常与其运行的OS相关.1.进程管理及安全相关的参数chroot <jail dir>修改haproxy的工 ...
- ACM解题之素矩阵
题意: 如果一个矩形的两条边都是素数,则称此矩形为素矩形.本题给出一个素矩形的面积,请计算其两条边的值.有多个测试用例.每个用例占一行,包含一个表示素矩形面积且不超过 108 的正整数.输入直至没有数 ...
- DevOps能力是落地微服务的前提
在软件开发领域不存在银弹,当用一项新的技术或新的架构时一定要明白其背后的原理,确保把合适的技术应用在合适的项目上,而不是盲目跟风. 单体应用伸缩性差,而且随着应用规模的扩大,业务逻辑和开发部署过程都变 ...
- Dual Boot WINDOWS 10 and KALI LINUX Easily STEP BY STEP GUIDE截图
mark. kali安装:https://www.youtube.com/watch?v=KLj2yQPWZDk 删除无用分区:http://www.xitongcheng.com/jiaocheng ...
- hdu1010感想
杭电这道题是用dfs走迷宫问题,一直wa是因为没有将走过的地方标记,所以如果遇到走迷宫的问题一定要将走过的地方标记,如下: &&nx<n&&ny>=& ...
- linux中获取堆栈空间大小的方法
#include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include < ...
- C#多线程学习之:Monitor类
关于对C#多线程类Monitor的理解 1.对线程的理解 围绕着锁周围的线程可以分为以下三类: l 拥有锁的线程:只有一个 l 就绪队列:只有就绪队列里的线程才有机会在锁被释放时去获取锁. l ...
- jack server 常见错误解决方法【转】
本文转载自:https://blog.csdn.net/qq_27061049/article/details/70156200 jack 服务常见错误解决方法 当你编译Android时,你不需要修改 ...