15% off all plans & roadmaps Use code
See plansLeetCode 78 · Solution & editorial
Given an integer array nums of unique elements, return all possible subsets (the power set).
Subsets is a Backtracking Patterns problem, in the Subsets (Include/Exclude) subpattern. The pattern covers subsets, permutations, combinations, N-Queens, and word search
Generate all subsets using include/exclude decision pattern. Handle duplicates in subsets.
The reference solution runs in O(n * 2^n) time and O(n * 2^n) space.
Subsets has been reported in interviews at Facebook, Bloomberg, Amazon, Uber, Microsoft, Apple, ByteDance.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Subsets is a Backtracking Patterns problem, in the Subsets (Include/Exclude) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.