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 ...
随机推荐
- stm32的复用与映射
摘自:https://blog.csdn.net/lincheng15/article/details/51789093 摘自:http://www.51hei.com/bbs/dpj-36242-1 ...
- 从源码角度实现一个自己的Promise
原文链接:https://geniuspeng.github.io/2017/12/14/my-promise/ 关于Promise的概念以及意义就不在这里介绍了,最近看到了一些实现Promise的核 ...
- 【47.40%】【codeforces 743B】Chloe and the sequence
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【2186】Popular Cows(强连通分支及其缩点)
id=2186">[2186]Popular Cows(强联通分支及其缩点) Popular Cows Time Limit: 2000MS Memory Limit: 65536 ...
- python opencv3 —— findContours
findContours 是 opencv 下的轮廓提取函数. 1. api 分析 findContours(image, mode, method[, contours[, hierarchy[, ...
- ArcSDE中Compress与Compact的区别
原文 ArcSDE中Compress与Compact的区别 附件一”为两种数据库需要的管理工作. 与所表示的含义与操作是不同的. 对于来说,Compressing与Smart Dat ...
- [Grid Layout] Use auto-fill and auto-fit if the number of repeated grid tracks is not to be def
What about the situation in which we aren’t specifying the number of columns or rows to be repeated? ...
- Spring MVC--@RequestMapping
2.1 @RequestMapping @RequestMapping是SpringMVC的核心注解,负责访问的url与调用方法之间的映射; @RequestMapping可以放在类和方法上; @Re ...
- js进阶 10-1 JQuery是什么
js进阶 10-1 JQuery是什么 一.总结 一句话总结: 1.两种引用jquery的方法? 可以在线jquery和本地jquery两种 2.jquery主要好处? 浏览器兼容问题 二.js进阶 ...
- js进阶 9-14 js如何实现下拉列表多选移除
js进阶 9-14 js如何实现下拉列表多选移除 一.总结 一句话总结: 1.js如何实现下拉列表多选移除? 把这个下拉列表中的option移除,然后加到另外一个下拉列表(文字)中去.remove方法 ...