Subsets
Medium · Backtracking · Array · Backtracking · Bit Manipulation
Problem
Given an array nums of distinct integers, return all possible subsets (the power set), in any order and without duplicates.
Examples
Input: nums = [1,2,3] Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
All 2³ = 8 subsets.
Input: nums = [0] Output: [[],[0]]
Constraints
- 1 <= len(nums) <= 10
- -10 <= nums[i] <= 10
- All values are distinct
Approaches
- Bitmask enumeration — time O(n · 2ⁿ), space O(1) extra
Related problems
Start free on Cariara · Coding problems · System design studio