Algorithm/java tip

78. Subsets

개구리는 개꿀개꿀 2021. 3. 7. 00:40

leetcode.com/problems/subsets/

 

Subsets - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

class Solution {
    List<List<Integer>> ret = new ArrayList<>();
    public List<List<Integer>> subsets(int[] nums) {
        List<Integer> subset = new ArrayList<>();
		BT(nums, 0, subset);
        return ret;
    }
    
    public void BT(int[] nums, int index, List<Integer> subset) {
		if(index == nums.length) {
			ret.add(new ArrayList<>(subset));
			return;
		}
		int c = nums[index];
		BT(nums, index + 1, subset);
		subset.add(c);
		BT(nums, index + 1, subset);
		subset.remove(subset.size() - 1);
	}
}

leetcode.com/problems/subsets/discuss/27281/A-general-approach-to-backtracking-questions-in-Java-(Subsets-Permutations-Combination-Sum-Palindrome-Partitioning)

 

A general approach to backtracking questions in Java (Subsets, Permutations, Combination Sum, Palindrome Partitioning) - LeetCod

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com