Java.Algo.Backtracking.AllSubsets

    public static List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        dfsBacktrackingAllSubsets(nums, 0, new ArrayList<>(), res);
        return res;
    }

    private static void dfsBacktrackingAllSubsets(int[] nums, int index, List<Integer> prevCombination, List<List<Integer>> allCombinations) {
        List<Integer> newCombination = new ArrayList<>(prevCombination);
        newCombination.add(nums[index]);
        allCombinations.add(newCombination);

        if (index == nums.length - 1) {
            return;
        }

        dfsBacktrackingAllSubsets(nums, index + 1, prevCombination, allCombinations);
        dfsBacktrackingAllSubsets(nums, index + 1, newCombination, allCombinations);
    }
This entry was posted in Без рубрики. Bookmark the permalink.

Leave a Reply

Your email address will not be published.