classSolution{ List<List<Integer>> res = new ArrayList<>(); public List<List<Integer>> combinationSum3(int k, int n) { int[] used = newint[9]; List<Integer> current = new ArrayList<>(); backTrace(1,current,k,n,used); return res; }
publicvoidbackTrace(int i, List<Integer> current, int k, int n, int[] used){ if(current.size() == k){ if(n == 0){ res.add(new ArrayList<>(current)); } return; } for(int j=i;j<=9;j++){ if(used[j-1]==0){ current.add(j); used[j-1] = 1; backTrace(j,current,k,n-j,used); used[j-1] = 0; current.remove(current.size()-1); } } } }