Given a set of distinct integers, I want to find all the possible subsets (for [1,2,3], the code should print [1], [1,2], [1,3], [1,2,3], [2], [2,3], [3]
(not necessarily in that order).
There are a few solutions (like this one) out there but what I want to do is to re-implement the bellow solution with a new recursion and no for
loop by passing around the indexes: (start = 0)
public void forSolution(List<List<Integer>> res, int[] nums, List<Integer> list, int start) {
for (int i = start; i < nums.length; i++) {
List<Integer> tmp = new ArrayList<>(list);
tmp.add(nums[i]);
res.add(new ArrayList<>(tmp));
forSolution(res, nums, tmp, i + 1);
}
}
I thought I need to pass two integers to the method, one for keeping the record of index and the other one for keeping the start point, but I am having problem on when I need to do the index increment (vs start increment). Any help would be appreciated.