I would like to ask this code for which the final return result suddenly becomes an empty array?

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: [ [], [] ]
Aug.02,2021

...
res.push(temp);
...
temp.pop();
...

the temp array in the push to res array is pop empty after recursion.


remember to make a shallow copy, otherwise all references are the same temp

.
 if (temp.length === nums.length) {
-  res.push(temp);
+  res.push(temp.slice());
   console.log(res);
}
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3b85c-2c292.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3b85c-2c292.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?