Skip to content

78.子集

题解

js
/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var subsets = function (nums) {
  const res = [];
  function fn(index, path) {
    res.push([...path]);
    if (index >= nums.length) {
      return;
    }
    for (let i = index; i < nums.length; i++) {
      const next = path.concat(nums[i]);
      fn(i + 1, next);
    }
  }
  fn(0, []);
  return res;
};
/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var subsets = function (nums) {
  const res = [];
  function fn(index, path) {
    res.push([...path]);
    if (index >= nums.length) {
      return;
    }
    for (let i = index; i < nums.length; i++) {
      const next = path.concat(nums[i]);
      fn(i + 1, next);
    }
  }
  fn(0, []);
  return res;
};