Practice · Backtracking · 2 of 4

All Subsets

You are given nums, a list of distinct integers. Return every subset of it: every list you can make by keeping some of the numbers, from none of them to all of them.

Each subset must appear exactly once. The subsets may come in any order, and so may the numbers inside each one.

Example 1

Input
nums = [4, 7, 1]
Output
[[], [4], [7], [1], [4, 7], [4, 1], [7, 1], [4, 7, 1]]
Each of the three numbers is in or out, which makes 2³ = 8 subsets, the empty one included.

Example 2

Input
nums = [-2]
Output
[[], [-2]]

Constraints

  • nums holds 1 to 10 numbers.
  • Every number is between −10 and 10, and no number appears twice.
Brush up firstSubsets visualiser