고유 하위 집합 배열

3263 단어 leetcodejavascript
var subsets = function (nums) {
  let index = 0;
  let subset = [];
  let tempArray = [];

  findSubset(index, nums, subset, tempArray);
  return subset;
};

const findSubset = (index, nums, subset, tempArray) => {
  if (index >= nums.length) {
    subset.push([...tempArray]);
    return;
  }

  findSubset(index + 1, nums, subset, tempArray);
  tempArray.push(nums[index]);
  findSubset(index + 1, nums, subset, tempArray);
  tempArray.pop();
};

console.log(subsets([1, 2, 3]));

좋은 웹페이지 즐겨찾기