**
**
解决方案集不得包含重复的子集。按任意顺序返回解决方案。
Example 1:
Input: nums = [1,2,3]
Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
Example 2:
Input: nums = [0]
Output: [[],[0]]
我的解决方案-
let subsets = function(nums) {
let output = []
function backtrack(nums, subset = [], index = 0) {
if(index === nums.length) {
output.push(subset)
return
}
// ith element is not considered
backtrack(nums, subset, index + 1)
// ith element is considered
subset.push(nums[index])
backtrack(nums, subset, index + 1)
subset.pop()
}
backtrack(nums)
return output
};
console.log(subsets([1,2,3]))
Output:
[
[],
[],
[],
[],
[],
[],
[],
[],
]
Expected Output:
[
[],
[1],
[2],
[3],
[1,2],
[1,3],
[2,3],
[1,2,3]
]
当我在回溯函数中推送其中的子集时,我不知道为什么输出数组为空。