Subsets

Backtracking, problem 2 of 7

Subsets

Medium

LC #78

subsetspower set

Not attempted yet

Given a list nums of distinct integers, return all of its subsets (the power set). The result must not contain the same subset twice. Return the subsets in any order; the numbers inside a subset may be in any order too.

Example 1

Input: nums = [1,2,3]
Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]

Example 2

Input: nums = [0]
Output: [[],[0]]

Example 3

Input: nums = [5,-1]
Output: [[],[5],[-1],[5,-1]]

Constraints

  • 1 ≤ len(nums) ≤ 10
  • -10 ≤ nums[i] ≤ 10
  • All numbers are distinct.

Python

Loading draft…

Test results

7 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

3 examples, 4 hidden

Run examples, then submit all tests.