Subsets

Problem

Given an integer array nums of unique elements, return all possible subsets (the power set).

The solution set must not contain duplicate subsets. Return the solution in any order.

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

Pseudocode

- don't understand question

Solution

// from solutions
let result;
var subsets = function (nums) {
  result = []; // no need this if function is executed once
  recursive(0, nums, []);
  return result;
};

var recursive = function (start, nums, subset) {
  //   console.log(subset)
  result.push(subset.slice(0));

  for (let i = start; i < nums.length; i++) {
    subset.push(nums[i]);
    // console.log(subset)
    recursive(i + 1, nums, subset);
    subset.pop();
    // console.log(subset)
  }
};

Time and Space Complexity

Time

  • What did the code do

  • Total -

Space

  • What did the code do

  • Total -

Last updated