没看懂Leetcode这道题的答案,请指点# JobHunting - 待字闺中
f*m
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?
谢谢。
(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?
谢谢。