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 ...
随机推荐
- static 静态
摘自:https://blog.csdn.net/Kendiv/article/details/675941 关于static的 ""记忆性"" 我们可以用做 ...
- 我的MFC/C++学习笔记 http://blog.bccn.net/CrystalFan/6909
2009.07.31 ------------------------------------------------------------------------------------ No.1 ...
- Cocos2d-x使用Javascript开发js绑定C++<代码演示样例>
class IOSiAPDelegate{ public: virtual ~IOSiAPDelegate() {} }; class IOSAlipay{ public: IOSAlipay(); ...
- php 微信支付企业付款
1.所需参数 字段名 变量名 必填 示例值 类型 描述 公众账号appid mch_appid 是 wx8888888888888888 String 公众号的appId 商户号 mchid 是 19 ...
- 在vue中使用nprogress
NProgress的官网:http://ricostacruz.com/nprogress/ 源码地址:https://github.com/rstacruz/nprogress 1.安 ...
- 【2001】关于N!的问题
Time Limit: 3 second Memory Limit: 2 MB 编写程序,计算n!以十进制数形式表示的数中最右边一个非零数字,并找出在它右边有几个零. 例如:12!=1*2*3*4*5 ...
- strong & weak 的理解
import "ViewController.h" @interface ViewController () /*weak*/ @property (nonatomic,weak) ...
- Fragment使用LocalBroadcastManager接收广播消息
这种方式不用在配置文件加东西 变量声明 LocalBroadcastManager broadcastManager; IntentFilter intentFilter; BroadcastRece ...
- 快来看看Google出品的Protocol Buffer,别仅仅会用Json和XML了
前言 习惯用 Json.XML 数据存储格式的你们,相信大多都没听过Protocol Buffer Protocol Buffer 事实上 是 Google出品的一种轻量 & 高效的结构化数据 ...
- 关于iPhone开发的一些建议
建议 以后的应用程序,都使用AutoLayout, 不要再用绝对定位CGReck. 使用类似网页的方式来设计界面. 设计师好,程序员也好,尽量使用点这个单位进行思考,而不要使用像素.比如,你需要做44 ...