由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 没看懂Leetcode这道题的答案,请指点
相关主题
网盘电面一道我发现我竟然学会了12种tree traversal的办法
一个小题 谁能帮着给点思路 谢谢啦!请教recursive backtracking问题的时间复杂度的分析
leetcode Palindrome Partitioning"简单的"linklist的问题
问一道leetcode上的题目 combination sum关于leetcode的combinationSum题
Combination Sum 时间和空间复杂度是多少?判断一个linked list是不是palindrome
我来问个面经:打印binary tree 从root到leaf的所有path有人同看Populating Next Right Pointers in Each Node II的recursive写法么?
关于permutation和combinationDFS 堆栈溢出,怎么破?
两种DPGoogle 电面面经
相关话题的讨论汇总
话题: sum话题: cand话题: candidate话题: int话题: target
进入JobHunting版参与讨论
1 (共1页)
f*********m
发帖数: 726
1
Print All Combinations of a Number as a Sum of Candidate Numbers
http://www.leetcode.com/2010/09/print-all-combinations-of-number-as-sum.html
原文如下,稍作修改(把target 从7该为9):
“To search for all combination, we use a backtracking algorithm. Here, we
use the above example of candidate={2,3,6,7} and target=9.
First, we start with a sum of 0. Then, we iterate over all possibilities
that can be added to sum, which yields the possible set of sum={2,3,6,7}.
Assume now that sum=2, we continue adding all possible candidate numbers {2,
3,6,7} to sum=2, which yields sum={4,5,8,9}. Each step we have to use an
array to keep track of all the indices of candidate numbers that add to the
current sum, so that we can print the combination later. The next case would
be sum=3. We look at all possible candidate numbers {3,6,7} to be added to
sum=3, which yields sum={6,9,10}. Note that there is no need to look
backward (ie, candidate numbers that are smaller than 3), as this would only
yield duplicate results. We keep doing this recursively, until we reach the
conditions below, where we stop.
。。。

看原文,sum只是candidate number中的一个,然后取出来candidate相加一次,得到一
个新的sum数组,比如,sum=3,则sum数组={6,9,10}。那么如何保证3可以被加3次,使
得9=3+3+3?
原文说“We keep doing this recursively”,请问如何recursive?
谢谢。
p*****2
发帖数: 21240
2
感觉你理解错了吧?
f*********m
发帖数: 726
3
二哥能否指点一二?
p*****2
发帖数: 21240
4
target is 7, candidate is 2,3,6,7
output should be 7 and 3+2+2 (but not print 2+3+2, 2+2+3)
不就是在一个数组里找subset,使得sum为target吗?
j********e
发帖数: 1192
5
正好做了这道题。
其实就是recursive,不过先把candidate排序,然后确保输出唯一结果
(例如一旦使用了3,以后就不能再使用比3小的数)
class Solution {
public:
vector > results;

void combinationSum(int sum, int target, const vector &cand,
int index, vector &queue) {
if(sum == target) {
results.push_back(queue);
return;
}

for(int i = index; i if(sum + cand[i] <= target) {
queue.push_back(cand[i]);

combinationSum(sum + cand[i], target, cand,
i, queue);

queue.pop_back();
}
else
break;
}

}

vector > combinationSum(vector &candidates, int
target) {
vector cand = candidates;
vector queue;

results.clear();
sort(cand.begin(), cand.end());
combinationSum(0, target, cand, 0, queue);

return results;
}
};

2,
the

【在 f*********m 的大作中提到】
: Print All Combinations of a Number as a Sum of Candidate Numbers
: (http://www.leetcode.com/2010/09/print-all-combinations-of-number-as-sum.html
: 原文如下,稍作修改(把target 从7该为9):
: “To search for all combination, we use a backtracking algorithm. Here, we
: use the above example of candidate={2,3,6,7} and target=9.
: First, we start with a sum of 0. Then, we iterate over all possibilities
: that can be added to sum, which yields the possible set of sum={2,3,6,7}.
: Assume now that sum=2, we continue adding all possible candidate numbers {2,
: 3,6,7} to sum=2, which yields sum={4,5,8,9}. Each step we have to use an
: array to keep track of all the indices of candidate numbers that add to the

f*********m
发帖数: 726
6
但是数组里的元素可以重复出现,比如数组中只含有一个2,而结果可以是2+2+3.所以
还是有些区别吧?

【在 p*****2 的大作中提到】
: target is 7, candidate is 2,3,6,7
: output should be 7 and 3+2+2 (but not print 2+3+2, 2+2+3)
: 不就是在一个数组里找subset,使得sum为target吗?

p*****2
发帖数: 21240
7

可以重复也没关系。道理差不多呀。就是取0个,或者取多个。然后往后找。

【在 f*********m 的大作中提到】
: 但是数组里的元素可以重复出现,比如数组中只含有一个2,而结果可以是2+2+3.所以
: 还是有些区别吧?

e***s
发帖数: 799
8
这题是不是DP更好一点。
a********d
发帖数: 195
9
是不是150上硬币的题加个传入的数组和打印条件?
f*********m
发帖数: 726
10
这道题需要排序吗?leetocode中说不需要(http://www.leetcode.com/2010/09/print-all-combinations-of-number-as-sum.html

【在 j********e 的大作中提到】
: 正好做了这道题。
: 其实就是recursive,不过先把candidate排序,然后确保输出唯一结果
: (例如一旦使用了3,以后就不能再使用比3小的数)
: class Solution {
: public:
: vector > results;
:
: void combinationSum(int sum, int target, const vector &cand,
: int index, vector &queue) {
: if(sum == target) {

f*********m
发帖数: 726
11
combinationSum(sum + cand[i], target, cand,i, queue);为什么不是
combinationSum(sum + cand[i], target, cand,i+1, queue)?

【在 j********e 的大作中提到】
: 正好做了这道题。
: 其实就是recursive,不过先把candidate排序,然后确保输出唯一结果
: (例如一旦使用了3,以后就不能再使用比3小的数)
: class Solution {
: public:
: vector > results;
:
: void combinationSum(int sum, int target, const vector &cand,
: int index, vector &queue) {
: if(sum == target) {

1 (共1页)
进入JobHunting版参与讨论
相关主题
Google 电面面经Combination Sum 时间和空间复杂度是多少?
what is the internal implementation of Deque我来问个面经:打印binary tree 从root到leaf的所有path
究竟什么定义了DP关于permutation和combination
Python大牛请进两种DP
网盘电面一道我发现我竟然学会了12种tree traversal的办法
一个小题 谁能帮着给点思路 谢谢啦!请教recursive backtracking问题的时间复杂度的分析
leetcode Palindrome Partitioning"简单的"linklist的问题
问一道leetcode上的题目 combination sum关于leetcode的combinationSum题
相关话题的讨论汇总
话题: sum话题: cand话题: candidate话题: int话题: target