Java8 solution with DP(Dynamic Programming)


  • 0
    R
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> subsets = new ArrayList<>();
        subsets.add(new ArrayList<>());
        
        for (int index = 0; index < nums.length; index++) {
            int currentNum = nums[index];
            
            List<List<Integer>> newSubsets = subsets.stream()
                    .map(subset -> {
                        List<Integer> newSubset = new ArrayList<>(subset);
                        newSubset.add(currentNum);
                        return newSubset;
                    })
                    .collect(Collectors.toList());
            subsets.addAll(newSubsets);
        }
        
        return subsets;
    }
    

Log in to reply
 

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