Skip to content

Files

Latest commit

4c12802 · May 20, 2018

History

History
65 lines (49 loc) · 997 Bytes

78. Subsets.md

File metadata and controls

65 lines (49 loc) · 997 Bytes

78. Subsets

  • Difficulty: Medium.
  • Related Topics: Array, Backtracking, Bit Manipulation.
  • Similar Questions: Subsets II, Generalized Abbreviation, Letter Case Permutation.

Problem

Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

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

Solution

/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var subsets = function(nums) {
  var res = [];
  helper(nums, res, [], 0);
  return res;
};

var helper = function (nums, res, arr, start) {
  var len = nums.length;
  
  res.push(Array.from(arr));
  
  if (start === len) return;
  
  for (var i = start; i < len; i++) {
    arr.push(nums[i]);
    helper(nums, res, arr, i + 1);
    arr.pop();
  }
};

Explain:

nope.

Complexity:

  • Time complexity :
  • Space complexity :