LeetCode 78. Subsets
π§© Problem Description
Given an integer array nums
of unique elements, return all possible subsets (the power set).
The solution set must not contain duplicate subsets.
π¬ Example
1 | Input: nums = [1,2,3] |
π‘ Approach: Backtracking
We explore each elementβs choice: include or exclude.
Alternatively, we can also use bitmask enumeration.
π’ Java Code (Backtracking)
1 | import java.util.*; |
β± Complexity Analysis
- Time: O(n Γ 2^n) β there are 2^n subsets, each takes up to O(n) to copy
- Space: O(n) recursion depth (excluding output storage)
βοΈ Summary
- Generates the power set of
nums
. - Backtracking and bitmasking are both standard approaches.
- Useful in combinatorial problems.
Related problems:
lc-90
β Subsets II (with duplicates)lc-46
β Permutationslc-39
β Combination Sumlc-40
β Combination Sum II