A general approach to backtracking questions in Java (Subsets, Permutations, Combination Sum, Letter Combinations of a Phone Number, Generate parentheses, Binary Watch Palindrome Partitioning)

https://discuss.leetcode.com/topic/46161/a-general-approach-to-backtracking-questions-in-java-subsets-permutations-combination-sum-palindrome-partitioning

public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        if(nums == null || nums.length == 0){
            return res;
        }
        List<Integer> set = new ArrayList<Integer>();
        Helper(nums, res, 0, set);
        return res;

    }
    public void Helper(int[] nums, List<List<Integer>> res, int pos, List<Integer> set){
        res.add(new ArrayList<Integer> (set));
        for(int i = pos; i < nums.length; i++){
            set.add(nums[i]);
            Helper(nums, res, i+1, set);
            set.remove(set.size() - 1);
        }
    }

results matching ""

    No results matching ""