78. 子集

117 阅读1分钟

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    
    public List<List<Integer>> subsets(int[] nums) {
        res.add(new ArrayList());
        dfs(nums, 0);
        return res;
    }

    public void dfs(int[] nums, int start) {
        if (start == nums.length) {
            return;
        }
        for (int i = start; i < nums.length; i++) {
            path.add(nums[i]);
            res.add(new ArrayList(path));
            dfs(nums, i + 1);
            path.removeLast();
        }
    }
}