import java.util.*;
public class Solution {
public ArrayList<ArrayList<Integer>> combinationSum2(int[] num, int target) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
Deque<Integer> path = new ArrayDeque<>();
int len = num.length;
Arrays.sort(num);
recur(num,0,target,path,res);
return res;
}
public void recur(int[] nums,int start,int target,Deque<Integer> path,ArrayList<ArrayList<Integer>> res){
if (target == 0){
ArrayList<Integer> array = new ArrayList<>(path);
res.add(array);
return;
}
if (start>=nums.length){
return;
}
for (int i = start; i < nums.length; i++) {
int num = nums[i];
if (i>start&&nums[i]==nums[i-1]) continue;
if (num<=target){
// target -=num;
path.addLast(num);
recur(nums,i+1,target-num,path,res);
path.removeLast();
}else {
continue;
}
}
}
}