You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.
The same repeated number may be chosen from candidates unlimited number of times.
Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
Example 1:
Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
[7],
[2,2,3]
]
Example 2:
Input: candidates = [2,3,5], target = 8,
A solution set is:
[
[2,2,2,2],
[2,3,3],
[3,5]
]
Given a set of candidate numbers (
candidates
) (without duplicates) and a target number (target
), find all unique combinations incandidates
where the candidate numbers sums totarget
.The same repeated number may be chosen from
candidates
unlimited number of times.Note:
target
) will be positive integers.Example 1:
Example 2:
这道题的常见解法是DFS+回溯,跟做过的permutations, subset等题目一样的思路,主要是要注意递归调用时各个变量的含义就可以了,不再赘述:
第二种递归写法是不用sum这个变量来计算,直接将target减去相应的值作为参数传递:
此外可以将List类型的tmp变量改位Stack类,会发现运行时间会小很多。
参考资料:
The text was updated successfully, but these errors were encountered: