Subsets
Medium Backtracking
nums is a list of different whole numbers.
A subset of nums is some of its numbers, from none of them to all of them, each used at most once. Two subsets are the same when they hold the same numbers, in whatever order.
Return every subset of nums, the empty one included, each exactly once. The subsets may come in any order, and so may the numbers inside each one.
Example 1
Input: nums = [4,-2]
One right answer: [[],[4],[-2],[4,-2]]
Each number is either in a subset or not, so two numbers make 2 × 2 = 4 subsets: none of them, 4 alone, -2 alone, and both.
Example 2
Input: nums = [7,0,5]
One right answer: [[],[7],[0],[7,0],[5],[7,5],[0,5],[7,0,5]]
Example 3
Input: nums = [-9]
One right answer: [[],[-9]]
Constraints
1 ≤ nums.length ≤ 10-100 ≤ nums[i] ≤ 100- All the numbers in
numsare different - The largest answer, for 10 numbers, has 1,024 subsets
Solve it here
Write it in Python, Java or C++, run it on your own cases, and submit against hidden tests.
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
See the AI roundWatch the explanation
More Backtracking
- Generate ParenthesesAI round · Medium
- Combination SumAI round · Medium
- PermutationsAI round · Medium
- Letter Combinations of a Phone NumberMedium
- Word SearchMedium