FE-Interview
FE-Interview copied to clipboard
Day287:给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。说明:解集不能包含重复的子集。
// 输入: nums = [1,2,3]
/*
输出
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
*/
每日一题会在下午四点在交流群集中讨论,五点小程序中更新答案 欢迎大家在下方发表自己的优质见解
二维码加载失败可点击 小程序二维码
扫描下方二维码,收藏关注,及时获取答案以及详细解析,同时可解锁800+道前端面试题。
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
};