Java 1ms


  • 0
    K

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

    private void subsets(List<List<Integer>> res, int[] nums, int start) {
        if (start == nums.length) {
            res.add(new ArrayList<>());
            return;
        }
        int num = nums[start];
        subsets(res, nums, start + 1);
        int len = res.size();
        
        for (int i = 0; i < len; i++) {
            List<Integer> added = new ArrayList<>(res.get(i));
            added.add(num);
            res.add(added);
        }
    } 
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.