Understanding Subsets
Return all subsets of an array of distinct integers. Include the empty subset. The order of subsets and elements within each subset does not matter.
How it works
A start index ensures that each combination is built in exactly one order.
Save the current path. For each remaining index, choose its value, recurse from the next index, and undo the choice.
Try an example
Input: nums = [1, 2, 3]
Output: [[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]]
Each element can be included or excluded, producing 2³ subsets.
Constraints
- At most 7 distinct integers.
- Every subset must appear exactly once.
Use the simulator
Change the input, then choose Visualize input. Use Next and Previous to inspect each decision, or Play to advance automatically. Compare approaches and their complexity. Choose Try yourself to write JavaScript, select My code and Visualize code to inspect its execution, or Run Tests to check results.
Execution happens in your browser. Drafts are saved locally; avoid entering secrets or sensitive information.