Subsets

MediumBacktracking

Given an array nums of distinct integers, return every possible subset of it (its power set).

A subset is any selection of the numbers, including the empty selection and the whole array. Two subsets with the same numbers are the same subset, so each must appear exactly once. You may return the subsets in any order, and the numbers inside each subset in any order.

Constraints

  • 0 <= nums.length <= 10
  • -10 <= nums[i] <= 100
  • All values in nums are different.

Examples

Example 1

Input: nums = [2,5,9]
Output: [[],[2],[5],[2,5],[9],[2,9],[5,9],[2,5,9]]

Three numbers give 2³ = 8 subsets: the empty one, three singles, three pairs and the whole array. Any order is fine, for the subsets and inside each subset.

Example 2

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

The empty subset always counts, so even one number gives two subsets.

Example 3

Input: nums = []
Output: [[]]

An empty array has exactly one subset: the empty one.

JavaScript you'll use: Recursion, the call stack and memoization Arrays: create, read, update Destructuring, spread and rest

/**
 * @param {number[]} nums
 * @return {number[][]}
 */
function subsets(nums) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.