幂集。编写一种方法,返回某集合的所有子集。集合中不包含重复的元素。
说明:解集不能包含重复的子集。
示例:
输入: nums = [1,2,3]
输出:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
代码
class Solution {List<List<Integer>> ress=new ArrayList<>();public List<List<Integer>> subsets(int[] nums) {sub(nums,0);
return ress;}public void sub(int[] nums,int loc) {List<List<Integer>> ss=new ArrayList<>();if(loc==nums.length)//递归边界{ress.add(new ArrayList<>());return ;}sub(nums, loc+1);List<List<Integer>> next=new ArrayList<>(ress);for(List<Integer> l:ress)//将后面的子集和当前的连接{ArrayList<Integer> na=new ArrayList<>(l);na.add(nums[loc]);next.add(new ArrayList<>(na));}ress=next;//刷新子集结果}
}