给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。
解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。

class Solution {
List<List<Integer>> res=new ArrayList<>();
LinkedList<Integer> path=new LinkedList<>();
public List<List<Integer>> subsets(int[] nums) {
if(nums.length==0){
res.add(new ArrayList<>());
return res;
}
subsetsHelp(nums,0);
return res;
}
private void subsetsHelp(int []nums,int startIndex){
res.add(new ArrayList<>(path));
if(startIndex>=nums.length){
return;
}
for(int i=startIndex;i<nums.length;i++){
path.add(nums[i]);
subsetsHelp(nums,i+1);
path.removeLast();
}
}
}
475

被折叠的 条评论
为什么被折叠?



