Subsets
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.
Examples
nums = [1,2,3][[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]nums = [0][[],[0]]nums = [1,2][[],[1],[2],[1,2]]Constraints
1 <= nums.length <= 10-10 <= nums[i] <= 10All the numbers of nums are unique.
š” Hints
Hint 1
At each step in recursion, you can choose to include or exclude an element.
Hint 2
Use backtracking: append current element to path, recurse for remaining elements, then backtrack (pop).
Hint 3
Every node visited in the decision tree represents a valid subset.
Algorithm Complexity
Official asymptotic Big-O runtime and auxiliary memory analysis
Backtracking / DFS state-space tree traversal. At each step, copy current subset into result, then iterate through remaining choices, push, recurse, and pop.
There are 2^N total subsets for N elements; each subset requires up to O(N) time to construct and copy into results.
Backtracking recursion call stack and current subset path array consume at most O(N) auxiliary space.
š Real-World Use Cases
Enumerating all combinations of active amenity and feature filters (Wi-Fi, pool, kitchen) to generate cache keys and evaluate pricing matching subsets.
š Dry Run
Call backtrack(0) with empty path current = []. Save [] to result.