题目:46. 全排列
我的代码:
```js
let output = []
let curOutput = []
let used
/**
* @param {number[]} nums
* @return {number[][]}
*/
var permute = function (nums) {
used = new Array(nums.length).fill(false)
dfs(nums, 0)
return output
};
function dfs(n, idx) {
if (idx === n.length) {
output.push([...curOutput])
return
}
for (let i = 0; i < n.length; i++) {
if (!used) {
curOutput.push(n)
used = true
dfs(n, idx + 1)
used = false
curOutput.pop()
}
}
}
```
我的结果:
https://i.imgur.com/V9A3Wb8.png
leetcode 结果(上一个输出的结果和这个结果在一起了):
https://i.imgur.com/cyLlK94.png https://i.imgur.com/xu6q1p0.png https://i.imgur.com/66fxX6i.png |
|