AAlgoLoopSpaced repetition for LeetCode
MEDIUMBacktrackingLeetCode ↗

Subsets

The key idea

Each element is independently either in or out of a subset, so the power set has 2^n members. A depth-first walk over an index makes a binary include/exclude decision at every position; recording the running subset at each node of that decision tree yields every subset exactly once.

Problem

Given an integer array nums of unique elements, return all possible subsets (the power set). The solution set must not contain duplicate subsets. You may return the answer in any order.

Constraints

Examples

Input: nums = [1,2,3] Output: [[],[1],[1,2],[1,2,3],[1,3],[2],[2,3],[3]]
Input: nums = [0] Output: [[],[0]]

Complexity

Time: O(n * 2^n) Space: O(n)

See the full solution

410310
Step-by-step visualization
Start free →

More Backtracking problems