the following code is the solution to the full arrangement of Leetcode-sharp46 strings. The problem is that when the last recursion ends, the returned res array suddenly becomes an empty array. Please answer ~ Thank you for your answer
/**
* @param {number[]} nums
* @return {number[][]}
*/
function backtrack(res, temp, nums) {
if (temp.length === nums.length) {
res.push(temp);
console.log(res);
}
for (let i = 0; i < nums.length; iPP) {
if (temp.includes(nums[i])) {
continue;
}
temp.push(nums[i]);
backtrack(res, temp, nums);
temp.pop();
}
}
var permute = function(nums) {
var res = [];
var temp = [];
backtrack(res, temp, nums);
return res;
};
var nums = [1, 2];
let res2 = permute(nums);
console.log(res2)
// Output: res:
var nums = [1, 2];
let res2 = permute(nums);
console.log(res2)
// : res2: [ [], [] ]