Combination Sum II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations incandidates where the candidate numbers sums totarget.
Each number incandidates 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:
Input:
candidates =
[10,1,2,7,6,1,5]
, target =
8
,
A solution set is:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]Example 2:
Analysis
Basically the same as Combination Sum, yet need to handle
1) duplicated elements
2) each element may only use once
Solution
Backtracking
基本和Combination Sum一样,但是(1)要处理重复元素的问题,并且(2)每个元素只能用一次
因此两点改变:
传入startIdx时,用i + 1,而不是i;
在每次循环结束后,如果有重复,则i++,跳过剩余的重复部分
用remain而不是target,sum减少参数个数
Last updated
Was this helpful?