78_Subsets
[Medium][Backtracking]
Given a set of distinct integers, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
Solution:
Idea:
There are subsets in total. Because each integer can be included or not.
For each integer, use recursion to get subsets for the two possibilities:
the subset includes the integer
the subset doesn't include the integer
For storing each solution, there are two ways:
initialize a list called sol, update the elements when we progress.
In the helper function, set a parameter called selected__so__far, which includes all the selected integers. Update this parameter when we progress.
Time Complexity: there are paths in the tree
Space Complexity: for the output
Code 1: use a list to update current solution
Code 2: use a parameter in the helper function to store current solution
Solution 2: Mit Manipulation
Last updated