classSolution{ public List<List<Integer>> combinationSum2(int[] candidates, int target) { List<Integer> list = new ArrayList<>(); List<List<Integer>> ans = new ArrayList<>(); boolean[] visited = newboolean[candidates.length]; Arrays.sort(candidates); backtricking(candidates,target,ans,list,visited,0); return ans; } publicvoidbacktricking(int[] candidates,int target,List<List<Integer>> ans, List<Integer> list, boolean[] visited,int start){ if(target <= 0){ if(target == 0) ans.add(new ArrayList<>(list)); return; }
//for loop for this layer for(int i = start; i < candidates.length;i++){ if(visited[i]) continue; //avoid the duplicate combinations if (i != 0 && candidates[i] == candidates[i - 1] && !visited[i - 1]) continue;