40 Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
Each number in C 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.
For example, given candidate set[10, 1, 2, 7, 6, 1, 5]
and target8
,
A solution set is:
这题跟39很像,不过每个数只能取一次,所以在进入下一层递归时,我们要传i + 1下去,表示从下一个开始取。这题还有一个需要注意的地方,因为输入的set会有重复,然后答案不能有重复,我们要去重。如果不去重的话我们就会有这种情况:例如题目的例子,我们在递归的过程中,会把【1‘,1’‘,6】和【1’‘,1’,6】都加到结果里。所以就不对了。去重还是跟模板题一样,先排序。
Last updated