문제 링크 : Permutations
/**
* @param {number[]} nums
* @return {number[][]}
*/
var permute = function(nums) {
let arr = [...nums]
let num = arr.length
const results = [];
if (num === 1) return arr.map(v => [v]);
arr.forEach((fixed, index, origin) => {
const rest = [...origin.slice(0, index), ...origin.slice(index + 1)];
const permutations = permute(rest, num - 1);
const attached = permutations.map(v => [fixed, ...v]);
results.push(...attached);
});
return results;
};