给定一组不同的整数 nums,返回所有可能的子集(幂集)。
注意事项:该解决方案集不能包含重复的子集。
例如,如果 nums = [1,2,3],结果为以下答案:
[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]
详见:https://leetcode.com/problems/subsets/description/

Java实现:

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> res=new ArrayList<List<Integer>>();
        List<Integer> out=new ArrayList<Integer>();
        Arrays.sort(nums);
        helper(nums,0,out,res);
        return res;
    }
    private void helper(int[] nums,int start,List<Integer> out,List<List<Integer>> res){
        res.add(new ArrayList<Integer>(out));
        for(int i=start;i<nums.length;++i){
            out.add(nums[i]);
            helper(nums,i+1,out,res);
            out.remove(out.size()-1);
        }
    }
}

 

相关文章:

  • 2021-09-13
  • 2022-03-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-18
  • 2022-12-23
猜你喜欢
  • 2021-06-07
  • 2021-11-29
  • 2022-12-23
  • 2021-08-22
  • 2021-07-28
  • 2021-05-24
  • 2022-12-23
相关资源
相似解决方案