diff --git a/46 Permutations.js b/46 Permutations.js index 467e1ee..47a6069 100644 --- a/46 Permutations.js +++ b/46 Permutations.js @@ -62,6 +62,28 @@ var generate = function(nums, index, visited, output, result) { } +// Another clear solution +var permute = function(nums) { + return permuteAux(nums, []); +}; + +var permuteAux = function(nums, partialNums) { + if(nums === null || nums.length === 0) { + return [partialNums]; + } + var listArrays = []; + for(var i = 0; i < nums.length; i++) { + var withoutI = nums.slice(0,i).concat(nums.slice(i + 1, nums.length)); + var partial = partialNums.concat([nums[i]]); + var sol = permuteAux(withoutI, partial); + if(sol.legnth !== 0) { + listArrays = listArrays.concat(sol); + } + } + return listArrays; +}; + +