Find ith permutation in javascript
问题 Given an array arr of size n , and and index 0<=i<n! I want to return the i'th permutation. I was able to write a method that gets all permutations: function permute (arr) { var permutations = []; if (arr.length === 1) { return [ arr ]; } for (var i = 0; i < arr.length; i++) { var subPerms = permute(arr.slice(0, i).concat(arr.slice(i + 1))); for (var j = 0; j < subPerms.length; j++) { subPerms[j].unshift(arr[i]); permutations.push(subPerms[j]); } } return permutations; } How do I trim it to