lgwebdream / FE-Interview

🔥🔥🔥 前端面试,独有前端面试题详解,前端面试刷题必备,1000+前端面试真题,Html、Css、JavaScript、Vue、React、Node、TypeScript、Webpack、算法、网络与安全、浏览器
https://lgwebdream.github.io/FE-Interview/
Other
6.77k stars 897 forks source link

Day287:给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。说明:解集不能包含重复的子集。 #1108

Open Genzhen opened 3 years ago

Genzhen commented 3 years ago
// 输入: nums = [1,2,3]
/* 
输出
[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]
*/

每日一题会在下午四点在交流群集中讨论,五点小程序中更新答案 欢迎大家在下方发表自己的优质见解

二维码加载失败可点击 小程序二维码

扫描下方二维码,收藏关注,及时获取答案以及详细解析,同时可解锁800+道前端面试题。

DaphnisLi commented 1 year ago
const permute = function (nums) {
  const res = []
  const used = {}

  const recursion = (path) => {
    if (path.length <= nums.length) {
      res.push(path)
    } else {
      return
    }

    for (const value of nums) {
      if (used[value]) {
        continue
      }
      used[value] = true
      recursion([...path, value])
      used[value] = false
    }
  }

  recursion([])

  return res
};