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], [] ]
Seen this question in a real interview before?
输出给出的数组的全部子数组,一道np问题,暴力递归即可
public class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>>res = new ArrayList<List<Integer>>();
List<Integer>list = new ArrayList<Integer>();
Arrays.sort(nums);
helper(res,list,nums,0);
return res;
}
public void helper(List<List<Integer>>res,List<Integer>list,int[]nums,int n){
if(n==nums.length){
res.add(new ArrayList<Integer>(list));
return ;
}
helper(res,list,nums,n+1);
list.add(nums[n]);
helper(res,list,nums,n+1);
list.remove(list.size()-1);
return ;
}
}
本文介绍了一个算法问题:如何找出给定整数数组的所有可能子集,并确保结果中不含重复子集。通过递归方法实现解决方案,该算法适用于面试场景。

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



