Joey LIU | NANTSOU


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        for (int i = 0; i <= nums.length; i++) {
            dfs(nums, 0, i, new ArrayList<Integer>(), res);
        }
        return res;
    }

    private void dfs(int[] nums, int idx, int length, List<Integer> tmp, List<List<Integer>> res) {
        if (tmp.size() == length) {
            res.add(new ArrayList<>(tmp));
        }
        for (int i = idx; i < nums.length; i++) {
            tmp.add(nums[i]);
            dfs(nums, i + 1, length, tmp, res);
            tmp.remove(tmp.size() - 1);
        }
    }
}