Given a collection of distinct integers, return all possible permutations.
    Example:
    Input: [1,2,3]
    Output:
    [
    [1,2,3],
    [1,3,2],
    [2,1,3],
    [2,3,1],
    [3,1,2],
    [3,2,1]
    ]

    1. /**
    2. * @param {number[]} nums
    3. * @return {number[][]}
    4. */
    5. var permute = function(nums) {
    6. const result = [];
    7. const size = nums.length;
    8. const permutations = function (current = [], remaining = []) {
    9. if (current.length >= size) {
    10. result.push(current.slice());
    11. }
    12. for (let i = 0; i < remaining.length; i++) {
    13. current.push(remaining[i]);
    14. permutations(current.slice(), remaining.slice(0, i).concat(remaining.slice(i + 1)));
    15. current.pop();
    16. }
    17. }
    18. permutations([], nums);
    19. return result;
    20. };

    Runtime: 68 ms, faster than 99.84% of JavaScript online submissions forPermutations.
    Memory Usage: 37.4 MB, less than 24.41% of JavaScript online submissions forPermutations.