Skip to content
DeepForgeby svx
All problems
MediumAlgorithmsal-029

Subsets Generation

Problem

Generate every subset of the integers in nums and return them as a list of lists.

Enumerate bitmasks from 0 to 2^n - 1 and include nums[i] whenever bit i is set, which yields a fixed deterministic order starting with the empty subset. Return a list containing only the empty list when nums is empty.

Hint

Each of the n elements independently has two choices: in the subset or out.

Test cases

case 1
in: [[1,2,3]]
expected: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
case 2
in: [[0]]
expected: [[],[0]]
case 3
in: [[]]
expected: [[]]
case 4
in: [[1,2]]
expected: [[],[1],[2],[1,2]]
Starter codePython
def subsets(nums):
    # Your code here
    pass

4 test cases · run your code against them in the editor