Subsets - shilpathota/99-leetcode-solutions GitHub Wiki
Leet Code Link - https://leetcode.com/problems/subsets/description/
Given an integer array nums of unique elements, return all possible subsets (the power set).
The solution set must not contain duplicate subsets. Return the solution in any order.
Input: nums = [1,2,3]
Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
Input: nums = [0]
Output: [[],[0]]
1 <= nums.length <= 10
-10 <= nums[i] <= 10
All the numbers of nums are unique.
- Use a recursive function to generate all subsets.
- At each step, decide whether to include the current element in the subset or not.
- Recur for the remaining elements after including and excluding the current element.
- Base case: When there are no more elements left, add the subset to the result.
- This approach builds subsets incrementally, including one element at a time.
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
backtrack(nums,0,new ArrayList<>(),result);
return result;
}
public void backtrack(int[] nums, int start, List<Integer> path, List<List<Integer>> result){
result.add(new ArrayList<>(path));
for(int i=start;i<nums.length;i++){
path.add(nums[i]);
backtrack(nums,i+1,path,result);
path.remove(path.size()-1);
}
}
}
📥Input: nums = [1, 2, 3]. 📚Step-by-Step Walkthrough:
- Initially, the result list is empty.
- We start with the initial call to the backtrack function with start = 0 and path = [].
- Inside the backtrack function:
- We add the current path (empty list) to the result. So, result = [[]].
- We iterate over the elements of nums starting from index start = 0.
- For the first element 1:
- We add 1 to the path, so path = [1].
- We make a recursive call to backtrack(1, [1]).
- Inside the recursive call with start = 1 and path = [1]:
- We add the current path [1] to the result. So, result = ], [1.
- We iterate over the elements of nums starting from index start = 1.
- For the second element 2:
- We add 2 to the path, so path = [1, 2].
- We make a recursive call to backtrack(2, [1, 2]).
- Inside the recursive call with start = 2 and path = [1, 2]:
- We add the current path [1, 2] to the result. So, result = ], [1], [1, 2.
- We iterate over the elements of nums starting from index start = 2.
- For the third element 3:
- We add 3 to the path, so path = [1, 2, 3].
- We make a recursive call to backtrack(3, [1, 2, 3]).
- Inside the recursive call with start = 3 and path = [1, 2, 3]:
- We add the current path [1, 2, 3] to the result. So, result = ], [1], [1, 2], [1, 2, 3.
- Since start = 3 equals the length of nums, we return from this recursive call.
- After returning from the recursive call with start = 2, we remove the last element from path, so path = [1, 2].
- We move to the next iteration for nums[2] (which is 3).
- We repeat the same steps for nums[2] = 3, and after the recursive call returns, we remove the last element from path again.
- We return from the initial call to backtrack with start = 1, so path = [1].
- We move to the next iteration for nums[1] (which is 2).
- We repeat the same steps, and after the recursive call returns, we remove the last element from path.
- We return from the initial call to backtrack with start = 0, so path = [].
- We have iterated through all elements of nums, and the process is complete.
- At the end, result contains all the subsets of [1, 2, 3], including the empty subset, which is ], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3.