leetcode 输出 bug, case 输出粘在一起了

9 天前
 albert0yyyy
题目: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[i]) {
curOutput.push(n[i])
used[i] = true
dfs(n, idx + 1)
used[i] = false
curOutput.pop()
}
}
}



```
我的结果:


leetcode 结果(上一个输出的结果和这个结果在一起了):

358 次点击
所在节点    全球工单系统
1 条回复
kingcanfish
8 天前
全局变量是这样的 你得在函数执行的最后手动清空

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1078512

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX