Description
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:
| 1 | Input: candidates = [2,3,6,7], target = 7, | 
Example 2:
| 1 | Input: candidates = [2,3,5], target = 8, | 
解法
最开始想用动态规划来解这道题,但因为要输出是哪些数组成的答案,故不太方便。采用深度优先搜索的一般方法即可解题。为了进行剪枝,需对数组进行从大到小排序,如果碰到比target值大的,那么显然之后的数都不需要进行搜索了。同时,需要注意重复查找的问题,比如 input:[2,3,6,7] 7时,可能出现[[2,2,3],[2,3,2],[3,2,2],[7]]的情况,为了解决重复,需要让搜索时按一定的顺序进行,此题的解决方案是下一个加入序列搜索的数一定大于等于当前搜索的数,引入start参数,这样就保证了结果的唯一性。
具体代码如下:
| 1 | class Solution { |