leetcode 78. 子集


一、题目

给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。

解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。

示例 1:
输入:nums = [1,2,3]
输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]

二、解法

class Solution {
    List> ans=new ArrayList<>();
    List cur=new ArrayList<>();

    public List> subsets(int[] nums) {
        dfs(nums,0);
        return ans;
    }

    void dfs(int[] nums,int index){
        ans.add(new ArrayList<>(cur));
        int n=nums.length;
        if(index==n) return;
        for(int i=index;i

相关