I was working on a solution to the following prompt: Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.
The following code passes the tests:
/**
* @param {number[]} nums
* @return {number[][]}
*/
const permute = function(nums) {
const results = [];
const backtrack = (first = 0) => {
if (first === nums.length) {
results.push([...nums]);
return
}
for (let i = first; i < nums.length; i++) {
nums = swap(nums, first, i);
backtrack(first + 1);
nums = swap(nums, first, i)
}
}
backtrack()
return results
};
const swap = (arr, i, j) => {
const temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
Consider an example for calculating the permutations where nums = [1, 2, 3]
.
The gap in my understanding is apparent when I try to pass in nums
to results.push()
instead of results.push([...nums])
, which returns the appropriate length result array, but is filled with [1, 2, 3]
array at each index. Why is it necessary to make a shallow copy of nums
here instead of just pushing nums
directly? My understanding is that at the point in the recursive call where the base case is satisfied, nums
will be whatever the current permutation is. Following the storing of our permutation, we backtrack/unswap nums
and continue. I suppose it must be the case that all permutations in the result
array will always reflect the current value of the reference element, but if that is the case, why is the final result array then filled with [1, 2, 3]
for each element?
I thought I previously had a strong understanding of passing by reference vs passing by value but now I am doubting that understanding. Any clarification on this would be greatly appreciated as I feel like I am almost there but can't quite get it to click.
Thanks for taking the time to read/help.