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.
Solution:  Use set to handle duplicates.
Better idea is to use a previous number to mark whether duplicates exist. The following solution works for num = [1, 1] and target = 2; since each time we call dfs() and the target is still great than 0, the previous is set to be -1. But if the target is less or equal to 0, we will not reset previous to -1, and the duplicates can be detected.

Comments

Popular posts from this blog

Maximum Gap

[ITint5] Maximum Subarray for a Circular Array

[CC150] Chapter 8 Object-Oriented Design