Skip to content
CodeShare
All problems

Subsets

List every possible subset of a set of numbers.

MediumBacktrackingBit manipulation

Given an array nums of distinct integers, return every possible subset (the power set), including the empty one.

The answer must not contain duplicate subsets. You can return the subsets in any order, and the numbers inside each subset in any order.

Examples

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]]

Constraints

  • 1 <= nums.length <= 10
  • -10 <= nums[i] <= 10
  • All numbers in nums are distinct.