Description
Given a collection of candidate numbers (candidates
) and a target number (target
), find all unique combinations in candidates
where the candidate numbers sums to target
.
Each number in candidates
may only be used once in the combination.
Note:
- All numbers (including
target
) will be positive integers. - The solution set must not contain duplicate combinations.
Example 1:
1 | Input: candidates = [10,1,2,7,6,1,5], target = 8, |
Example 2:
1 | Input: candidates = [2,5,2,1,2], target = 5, |
解法
这题只需要在Leetcode(39)上做一点点的改动,这里不允许一个数重复利用,故只需要将下一次搜索的起点改为i+1即可,同时考虑去除重复输出的情况,即在start之后进行分叉搜索时,若nums[i] = nums[i-1]时,说明已经考虑过了,直接进入下一次循环。
具体代码如下:
1 | class Solution { |