public List<List<Integer>> combinationSum2(int[] cand, int target) {
Arrays.sort(cand);
List<List<Integer>> res = new ArrayList<List<Integer>>();
List<Integer> path = new ArrayList<Integer>();
dfs_com(cand, 0, target, path, res);
return res;
}
void dfs_com(int[] cand, int cur, int target, List<Integer> path, List<List<Integer>> res) {
if (target == 0) {
res.add(new ArrayList(path));
return ;
}
if (target < 0) return;
for (int i = cur; i < cand.length; i++){
if (i > cur && cand[i] == cand[i1]) continue;
path.add(path.size(), cand[i]);
dfs_com(cand, i+1, target  cand[i], path, res);
path.remove(path.size()1);
}
}
Java solution using dfs, easy understand


For those who don't understand how to avoid duplicate by:
if "(i > cur && cand[i] == cand[i1]) continue;when we should skip a number? not just it's the same as previous number, but also when it's previous number haven't been added!
i > cur means cand[i  1] is not added to the path (you should know why if you understand the algorithm), so if cand[i] == cand[i1], then we shouldn't add cand[i].
This tricky is very smart.

For those who don't understand how to avoid duplicate by: if "(i > cur && cand[i] == cand[i1]) continue;
when we should skip a number? not just it's the same as previous number, but also when its previous number haven't been added!
i > cur means cand[i  1] is pop from path (you should know why if you understand the algorithm), so if cand[i] == cand[i1], then we shouldn't add cand[i].
This tricky is very smart.

[1,1,1]
Let's think when should we skip the third 1, only when we add the first 1 but has not added the second 1, which implies that we only need to use one "1", rather than two "1", so skip the third "1".If we need to use two or three "1", the second "1" is used, then the third "1" won't be skipped .
As for your concern, if the first and second "1" are selected, do we skip the third ? of course not, because we may use three "1".