Subsets

Medium Backtracking AI round

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 nums are different
  • The largest answer, for 10 numbers, has 1,024 subsets

Watch the explanation

More Backtracking

All Backtracking problems