Leetcode dfs Combination Sum
Combination Sum
Total Accepted: 17319 Total
Submissions: 65259My Submissions
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where
the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.
Note:
- All numbers (including target) will be positive integers.
- Elements in a combination (a1, a2,
… , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤
… ≤ ak). - The solution set must not contain duplicate combinations.
For example, given candidate set 2,3,6,7
and
target 7
,
A solution set is:
[7]
[2, 2, 3]
题意:给定一组数C和一个数值T,在C中找到全部总和等于T的组合。
C中的同一数字能够拿多次。找到的组合不能反复。
思路:dfs
每一层的第i个节点有 n - i 个选择分支
递归深度:递归到总和大于等于T就能够返回了
复杂度:时间O(n!)。空间O(n)
感觉測试数据有问题,我用以下两个代码。对于[1,1],1这个输入。输出的结果各自是[[1],[1]]和[[1]],但两个代码都 Accepted 了。我感觉第二个代码才是正确的,输出结果没反复。
//代码一
vector<vector<int> > res;
vector<int> _nums;
void dfs(int target, int start, vector<int> &path){
if(target == 0) res.push_back(path);
for(int i = start; i < _nums.size(); ++i){
if(target < _nums[i]) return ; //这里假设没剪枝的话会超时
path.push_back(_nums[i]);
dfs(target - _nums[i], i, path);
path.pop_back();
}
} vector<vector<int> >combinationSum(vector<int> &nums, int target){
_nums = nums;
sort(_nums.begin(), _nums.end());
vector<int> path;
dfs(target, 0, path);
return res;
}
//代码二
vector<vector<int> > res;
vector<int> _nums;
void dfs(int target, int start, vector<int> &path){
if(target == 0) res.push_back(path);
int previous = -1;
for(int i = start; i < _nums.size(); ++i){
if(_nums[i] == previous) continue;
if(target < _nums[i]) return ; //这里假设没剪枝的话会超时
previous = _nums[i];
path.push_back(_nums[i]);
dfs(target - _nums[i], i, path);
path.pop_back();
}
} vector<vector<int> >combinationSum(vector<int> &nums, int target){
_nums = nums;
sort(_nums.begin(), _nums.end());
vector<int> path;
dfs(target, 0, path);
return res;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
Leetcode dfs Combination Sum的更多相关文章
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- [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] 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] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- 从Leetcode的Combination Sum系列谈起回溯法
在LeetCode上面有一组非常经典的题型--Combination Sum,从1到4.其实就是类似于给定一个数组和一个整数,然后求数组里面哪几个数的组合相加结果为给定的整数.在这个题型系列中,1.2 ...
- Leetcode dfs Combination SumII
Combination Sum II Total Accepted: 13710 Total Submissions: 55908My Submissions Given a collection o ...
随机推荐
- POJ 2590 Steps (ZOJ 1871)
http://poj.org/problem?id=2590 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1871 题目大 ...
- 每天一个JavaScript实例-操作元素定位元素
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- NoSql中的B-tree、B+tree和LSM-tree 分类: B7_HBASE 2015-03-15 18:27 85人阅读 评论(0) 收藏
总结: 1.B+树将数据完全排序,读数据时很快,但当要修改数据时,就需要将新入数据下面的数据重新排位,特别是当写入的数据排在较高的位置时,需要大量的移位操作才能完成写入. 2.SLM牺牲部分的读性能, ...
- JNI——Java调用C/C++函数
从C/C++到Java,再从Java回到C/C++,今天终于有机会了解了连接Java.C/C++的桥梁——JNI.哈哈!分享一下! 一.简介 JNI是Java native interface的简 ...
- Linux下安装Oracle11G(虚拟机)
1.内存设置为2G及以上2.设置swap: (1)root登录 (2)建立swap文件,如在/tmp下建立swapfree作为交换文件. # cd /tmp #dd if=/dev/zero of=s ...
- 魔兽争霸war3心得体会(四):不死族vs人族1本火魔塔
QQ对战平台上玩随机的人特别多,为了应对对方的"出其不意",我最近一直用小狗去探路,小狗在家采集30个木头-摆放商店,就可以去探路了.主要有几个好处:知道对方的种族-出生点位-开局 ...
- Matlab-------regexp正则表达式
转自原文 Matlab-------regexp正则表达式 句点符号 '.' ——匹配任意一个(只有一个)字符(包括空格). 例如:t.n,它匹配tan. ten.tin和ton,还匹配t#n.tpn ...
- [Angular2 Router] Get activated router url
getActivatedRoutePath(r: ActivatedRoute) { return r.url .subscribe(p => this.curtPath = p[0].path ...
- ios开发网络学八:NSURLSession相关代理方法
#import "ViewController.h" @interface ViewController ()<NSURLSessionDataDelegate> /* ...
- 使用Opencv中matchTemplate模板匹配方法跟踪移动目标
模板匹配是一种在图像中定位目标的方法,通过把输入图像在实际图像上逐像素点滑动,计算特征相似性,以此来判断当前滑块图像所在位置是目标图像的概率. 在Opencv中,模板匹配定义了6种相似性对比方式: C ...