Subsets
MediumBacktracking · Adobe
Given an array nums of distinct integers, return every subset of it (its power set). A subset is any selection of the values, from none of them (the empty subset []) to all of them.
Each subset must appear exactly once. Return the subsets in any order; the values inside a subset may also be in any order ([1,2] and [2,1] are the same subset).
Example 1
- Input:
- nums = [4,7,9]
- Output:
- [[],[4],[7],[4,7],[9],[4,9],[7,9],[4,7,9]]
- Explanation
- Each value is either in or out of a subset, so 3 values give 23 = 8 subsets, from
[]up to[4,7,9].
Example 2
- Input:
- nums = [-1,2]
- Output:
- [[],[-1],[2],[-1,2]]
- Explanation
- Two values give four subsets: neither, each one alone, and both.
Example 3
- Input:
- nums = [3]
- Output:
- [[],[3]]
- Explanation
- One value gives the empty subset and the subset containing it.
Constraints
1 <= nums.length <= 10-10 <= nums[i] <= 10- All values in
numsare distinct