We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
刷全排列这道题时,采用dfs递归把每次找到的排列结果存入ans时,得到的是长度为n的空数组。
function dfs(nums, len, temp) { if(temp.length === len) { ans.push(temp) return } ... }
ans结果为空数组,是因为我们直接push的temp只是每个递归的活动变量,一旦函数执行完毕,就会销毁temp变量,又因为temp是数组对象而不是值变量,ans中存储的是temp的引用地址,temp销毁后这块地址的值就为null了。 所以正确的做法是:
if(temp.length === len) { ans.push([...temp]) return }
生成新的对象就不会被销毁啦。[...temp]相当于生成新的数组对象。
[...temp]
The text was updated successfully, but these errors were encountered:
No branches or pull requests
刷全排列这道题时,采用dfs递归把每次找到的排列结果存入ans时,得到的是长度为n的空数组。
ans结果为空数组,是因为我们直接push的temp只是每个递归的活动变量,一旦函数执行完毕,就会销毁temp变量,又因为temp是数组对象而不是值变量,ans中存储的是temp的引用地址,temp销毁后这块地址的值就为null了。
所以正确的做法是:
生成新的对象就不会被销毁啦。
[...temp]
相当于生成新的数组对象。The text was updated successfully, but these errors were encountered: