Given a set of distinct integers, nums, return all possible subsets.
Note: The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,3]
, a solution is:
[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]
public class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res=new ArrayList<>();
List<Integer> list=new ArrayList<>();
helper(nums,0,res, (ArrayList<Integer>) list);
return res;
}
public void helper(int[] nums,int index,List<List<Integer>> res,ArrayList<Integer> list){
res.add(new ArrayList<>(list));
for(int i=index;i<nums.length;i++){
list.add(nums[i]);
helper(nums,i+1,res,list);
list.remove(list.size()-1);
}
}
}